Skip to content

Instantly share code, notes, and snippets.

@GT3000
Created April 13, 2021 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GT3000/4efeac1530e4dea05729210f4f2dfeeb to your computer and use it in GitHub Desktop.
Save GT3000/4efeac1530e4dea05729210f4f2dfeeb to your computer and use it in GitHub Desktop.
Cooldown System Example Firing Script
public class CooldownExample : MonoBehaviour
{
private float timeBetweenShots = 1.0f;
private float timePassed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Fire();
}
private void Fire()
{
//Add to timePassed
timePassed += Time.deltaTime;
//When player clicks on mouse button and timePassed is greater than timeBetweenShots fire a sphere
//First reset timePassed, then create sphere, add a rigidbody, turn off gravity, and add a force to shoot it up
if (Input.GetMouseButtonDown(0) && timePassed >= timeBetweenShots)
{
timePassed = 0f;
GameObject tempShot = GameObject.CreatePrimitive(PrimitiveType.Sphere);
tempShot.AddComponent<Rigidbody>();
tempShot.GetComponent<Rigidbody>().useGravity = false;
tempShot.GetComponent<Rigidbody>().AddForce(Vector3.up * 25, ForceMode.Impulse);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment