Created
April 13, 2021 07:46
-
-
Save GT3000/4efeac1530e4dea05729210f4f2dfeeb to your computer and use it in GitHub Desktop.
Cooldown System Example Firing Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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