- Home /
Shooting projectiles once per second.
I am trying to get my game to "shoot" a projectile out from the location of an empty object. As of now it shoots every two seconds BUT it shoots a ton at once.
Here is my code for shooting.
function Shoot(seconds) { if(seconds!=savedTime) { var bullet : GameObject = Instantiate(bulletPrefab ,transform.Find("projRelease").transform.position ,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 1000);
savedTime=seconds;
}
}
And here is the part for seconds.
function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
I am also getting this error.
Invalid Cast Exception : Cannot cast from source type to destination type.
Thanks in advance :)
Answer by Eric5h5 · Dec 23, 2010 at 03:25 AM
function Start () { InvokeRepeating("Shoot", 1.0, 1.0); }
function Shoot () { //Do shooty stuff here }
Im trying to get it to do it once per 2 seconds. Tried that, didnt work. BTW Im developing it for iPhone.
Never $$anonymous$$d, it works, forgot to remove seconds from parantheses
Your answer
![](https://koobas.hobune.stream/wayback/20220613092210im_/https://answers.unity.com/themes/thub/images/avi.jpg)