How to add a delay?

Hi

I’m making an arrow trap that is supposed to fire with a delay between the shots so that the player can walk past it. I know how to make it fire the arrow but how do I add a delay? Please help me!

Thanks in advance,
Redoxe

You should read up on coroutines.

Or it could be easier to you InvokeRepeating.

First off I’d like to say thanks for the help but I have a few questions:
When using the script from your second link the arrow would spawn in the middle of the screen and not at the arrow trap, how do I fix this?

I couldn’t really understand the first link, which example will add a delay to the arrow and how will it know that it is supposed to fire the right object from the right place?

Sorry for being a newb.

look up Waitforseconds in the help

InvokeRepeating("ShootArrow", 2.0, 0); //call the ShootArrow function at 2 second intervals

function ShootArrow(){
   //shoot arrow stuff goes here
}

Hmm, I’m not getting any error but nothing is happening. Did I do something wrong?

Arrowtrap.js

var Arrow : GameObject;

InvokeRepeating("ShootArrow", 2.0, 0); 

function ShootArrow(){
   var the_Arrow = Instantiate(Arrow); 
      the_Arrow.transform.position = transform.position; 
      the_Arrow.transform.rotation = transform.rotation; 
}

Arrow.js

var speed : float; 
var timer : float; 
 
function Start() 
{ 
   timer = Time.timeSinceLevelLoad + 5.0f;
   
   } 
 
function Update () 
{ 
   transform.position += speed * Time.deltaTime*transform.forward;

   if(timer < Time.timeSinceLevelLoad) 
      Destroy(gameObject); 
	
	   
}

InvokeRepeating(“ShootArrow”, 2.0, 0); needs to be placed inside of a function, like in:

function Start()
{
}

Like this?

var Arrow : GameObject;

function Start()
{
InvokeRepeating("ShootArrow", 2.0, 0); 
}

function ShootArrow(){
   var the_Arrow = Instantiate(Arrow); 
      the_Arrow.transform.position = transform.position; 
      the_Arrow.transform.rotation = transform.rotation; 
}

Sadly, it’s not working. Is there something else that could cause a problem?

InvokeRepeating doesn’t need to be inside any functions, your first example should be fine; at first glance I don’t see anything wrong with it.

Try taking the destruction stuff out of the arrow script, making the whole script just:

var speed : float; 

function Update (){ 
   transform.position += speed * Time.deltaTime*transform.forward;
}

just to see if it works.

It works! I noticed that the Arrow didn’t show up when I moved the prefab to the scene so I deleted it and imported it from Maya again. There is one problem though, the arrow is facing the wrong way no matter how much I rotate it.

Oh, sorry I got the variables in invokerepeating mixed up… it should be:

InvokeRepeating("ShootArrow", 0.0, 2.0);

The first parameter is the initial delay (how long to wait after scene start to fire the first arrow), the second parameter is the delay between subsequent calls.

Thanks, got the delay working now and I think I know what is causing the arrow to be turned the wrong way.

the_Arrow.transform.position = transform.position; 
the_Arrow.transform.rotation = transform.rotation;

Since the arrow trap isn’t turned in the same direction as the arrow trap it will never turn right unless I rotate it in Maya and import it again.