This is code for simple shooting sphere when you click Fire1 button.
Now it shoots it straight after I click the button, but what I want to do, is make it shoot after like 2 seconds or so, because I have animation that plays shooting arrow on Fire1 click, and i want the sphere to be shot out like 2 seconds or so from the start of animation, not straight at the start
Coroutines are your friend here. Now I haven’t tested this, and I mainly work in C#, but this should at least point you in the right direction. It basically calls the function below which waits two seconds before doing anything. You could extend it out for different timings for different weapons as well if needed.
var projectile : Rigidbody;
var speed = 20;
var firing : boolean = false;
function Update () {
if (!firing Input.GetButtonUp ("Fire1")) {
// We need a way of knowing if the coroutine is already waiting
firing = true;
FireInTwoSeconds();
}
}
function FireInTwoSeconds() {
// This function will commence in 2 seconds
Debug.Log("Starting firing, waiting for 2 sec.");
yield WaitForSeconds(2);
Debug.Log("Now Firing!");
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
firing = false;
}
Here’s another option for delaying the firing by 2 seconds. I haven’t tested this code but have used the pattern in places where I don’t want to use a coroutine.
var projectile : Rigidbody;
var speed = 20;
var fireTime : float;
var fireing: boolean;