I want to edit it so it can only shoot a ball every 5 seconds. In other words, i want there to be a minimum 5 second interval between 2 ball shoots. How to make that happen? Thanks in advance.
This requires your ball to have a rigid body component. Make sure you read the anotations to understand what the script does.
//this is where we will specify the cannonball prefab in the inspector
var CannonballPrefab : Rigidbody;
//a boolean is a yes/no or true/false value. This will be switched on and off in this script
var CanFire : boolean = true;
//length of time it will take in sconds before the player can fire again
var CountdownTime : float = 5;
//this is how fast your cannonball will be fired, change this in the inspector depending on how big your level is
var CannonballSpeed : float;
//change this depending on how big your cylinder is,this is for if it's scaled by 1
//called every frame
function Update()
{
//if the mouse button is pressed and fire is allowed
if (Input.GetMouseButtonDown(0) && CanFire == true)
{
//player is not allowed to fire again until CanFire equals true
CanFire = false;
//start the function called Countdown
Countdown();
//this is where you instantiate the CannonballPrefab which you should have set, for the script, in the inspector
//this line states that the CannonballPrefab will be instantiated on the "up/Y" axis of the Cannon, the offset is so that it comes out of the end.
var Cannonball = Instantiate(CannonballPrefab, transform.position, transform.rotation);
//this line gives the Cannonball, which we have instatiated, a direction and speed to go.
Cannonball.rigidbody.velocity = transform.up * CannonballSpeed;
//this is a debug log, you can see if the script has worked as a message will pop up in the bottom right corner of the Unity editor
Debug.Log ("Cannon has just fired");
}
}
//called when player presses mouse button will not be called if it's already in progress
function Countdown ()
{
//this line waits for the amount of seconds that you set the variable CountdownTime to
yield WaitForSeconds (CountdownTime);
//player is allowed to fire
CanFire = true;
//this debug log appears when the player is allowed to fire again
Debug.Log ("Fire enabled");
}