x


RayCast Delay?

How would I go about scripting a delay for a raycast? Initially how would I change this script so that instead of it shooting the raycast every frame I can change the tempo of it?

    function Update(){
    if (Input.GetButton("Fire1")){
        Shoot();
    }
}

var shotSound: AudioClip; 
var bloodPrefab: GameObject; 
var sparksPrefab: GameObject; 

function Shoot(){
    if (shotSound) audio.PlayOneShot(shotSound); 
    var hit: RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, hit)){
      
        var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
        if (hit.transform.tag == "Enemy"){ 
            if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); 
            hit.transform.SendMessage("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver); 
        } else { 
            if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
        }
    }
}
more ▼

asked Nov 14 '11 at 08:04 PM

Dreave gravatar image

Dreave
137 82 95 96

Dammit you people stop double posting! It wastes people's time.

Nov 14 '11 at 09:13 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You could do something like split the shooting off into a separate Coroutine, which gets fired off from Start()

function ShotPoller()
{
    while(true)
    {
        if(Input.GetButton("Fire1"))
        {
            Shoot();
            yield WaitForSeconds(refireRate);
        } else {
            yield;
        }
    }
}


function Start()
{
    StartCoroutine(ShotPoller());
}

Then just modify 'refireRate' until it shoots at the right speed!

more ▼

answered Nov 14 '11 at 09:12 PM

syclamoth gravatar image

syclamoth
15k 7 15 80

would I put this as a differant script or would I add it onto the other one?

Nov 14 '11 at 09:20 PM Dreave

No, add it as part of the existing script. You wouldn't be able to call 'Shoot' if it were in a different script!

Nov 14 '11 at 09:38 PM syclamoth

I have an error saying "unknown identifyer: refireRate" can you help?

Nov 15 '11 at 06:45 PM Dreave

Well, obviously you have to declare a variable called refireRate somewhere! I didn't write your entire script for you, you need to know how to adapt this stuff! refireRate should be a public variable that you can change in the inspector (or from another script) so that you have fine control over your shoot speed.

Nov 15 '11 at 10:38 PM syclamoth

Im sorry if i have annoyed you, its just im pretty pathetic at scripting and im a still learning. one more question would this variable be a bollean?

Nov 16 '11 at 07:54 AM Dreave
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x3890
x1578

asked: Nov 14 '11 at 08:04 PM

Seen: 999 times

Last Updated: Nov 17 '11 at 09:52 PM