- Home /
The question is about timer and has been answered many times already.
How to put a pause in a function?
Alright, so i want the game to play out as: player shoots > has to wait 2 seconds > player can shoot again. Here is my code, I'm using raycast shooting with function updates, however i can't put a pause in there. help! #pragma strict
var Effect : Transform;
var TheDammage = 100;
var Canshoot = true;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5+Random.Range(-6,6), Screen.height*0.5+Random.Range(-6,6), 0));
if (Input.GetMouseButtonDown(0) && (Canshoot === true))
{
if (Physics.Raycast (ray, hit, 100))
{
//this is where the shooting happens.
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
Answer by Jamora · Aug 27, 2013 at 12:54 PM
You just need to call a method after 2 seconds that allows you to shoot again. I would add the following lines of code:
function AllowShooting(){
Canshoot = true;
}
//these go inside your raycast, possibly below the SendMessage
Invoke("AllowShooting",2);
Canshoot = false;
I used this Invoke method and it worked very well for me in creating pauses between successive function calls.
Answer by KiraSensei · Aug 27, 2013 at 12:54 PM
Try this :
if (Input.GetMouseButtonDown(0) && (Canshoot === true))
{
if (Physics.Raycast (ray, hit, 100))
{
//this is where the shooting happens.
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
WaitUntilNextShoot(2.0f);
}
}
And Make a new method :
function WaitUntilNextShoot(timer:float) {
Canshoot = false;
yield WaitForSeconds(timer);
Canshoot = true;
}
Answer by beayfergm · Aug 27, 2013 at 12:58 PM
You can use a "WaitForSeconds":
var Effect : Transform;
var TheDammage = 100;
var Canshoot = true;
var timeBetweenShots = 2.0f;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5+Random.Range(-6,6), Screen.height*0.5+Random.Range(-6,6), 0));
if (Input.GetMouseButtonDown(0) && (Canshoot === true))
{
Canshoot = false;
WaitToShoot();
if (Physics.Raycast (ray, hit, 100))
{
//this is where the shooting happens.
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
function WaitToShoot() {
// Waits "timeBetweenShots" seconds
yield WaitForSeconds (timeBetweenShots);
Canshoot = true;
}
Follow this Question
Related Questions
How to add a delay to my script. 3 Answers
keybind problem 0 Answers
Gun and hand animation tutorial! 2 Answers
Shooting. Bullet floats and sprays 1 Answer
FPS - Gun Delay while Rotating 0 Answers