- Home /
How to Delay A Ball shooting script
I am shooting A ball using a prefab. I have it set up so that when I press the Fire1 Key A animation plays, and in this script I made it plays at the same time as the animation, and I want the ball to shoot after the animation, so I tried to delay it using yield WaitForSeconds, but when I did that the script doesn't make the prefab any more any and so nothing happens, except the animation. I'm pretty new to Scripting. any help is appreciated.
var projectile : Rigidbody;
var speed = 20;
function FixedUpdate()
{
if(Input.GetButtonDown ("Fire1"))
yield WaitForSeconds(5);
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0,0,speed));
}
First you have to read manual for this topic. WaitForSeconds
You have to create new function for instantiating projectile and in that you can use WaitForSeconds as Romano says.
Answer by Romano · May 20, 2014 at 03:59 AM
I'm not totally sure but maybe try taking the yield part out of fixed update, I'm not sure if the updates like yield.
function FixedUpdate()
{
if (Input.GetButtonDown ("Fire1"))
{
FireFunction();
}
}
function FireFunction()
{
yield WaitForSeconds(5);
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3(0,0,speed));
}