- Home /
Question by
Hewhoisseth · Jun 15, 2015 at 05:40 AM ·
raycastdelay
Raycasting delay when shooting?
Can someone help me modify my script so that it has an adjustable delay in between shots? For instance, I would like a one second delay added to this script: Thanks for your help!
pragma strict
var Effect : Transform; var TheDammage = 100;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
Comment
Answer by davide2001 · Nov 14, 2015 at 07:27 PM
yeah it is not very hard. You must add a variable boolen. What do you want it is a firerate: your script with the fireRate can be this:
var Effect : Transform;
var TheDammage = 100;
var fireRate : float;
var canFire : boolean;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, hit, 100)&&canFire==false)
{
canFire=true;
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
yield WaitForSeconds (fireRate);
canFire=false;
}
}
Your answer
Follow this Question
Related Questions
How to Fix a Guns Firing Script so it Doesn't Constantly Fire 3 Answers
door that closes by itself 2 Answers
Raycast Delay 3 Answers