- Home /
Spawn on a timer
I have code that will spawn what I want on screen but there are two things I need to fix. One I need to spawn based on a timer after you hold the mouse down and have it spawn in a certain location. Can help will be useful, Thank you
var crystal : Transform;
function Update ()
{
var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
if ( Input.GetMouseButtonDown(0))
{
var crate = Instantiate(crystal, ray.origin, Quaternion.identity);
}
}
Answer by MangoDerp · May 03, 2012 at 03:32 AM
private float timer = 0.0f;
public float timeToHoldDownMouseBeforeSpawn = 3.0f;
.
.
.
.
if ( Input.GetMouseButton(0))
timer+= time.deltaTime;
if( Input.GetMouseButtonUp(0))
timer = 0;
if(timer >= timeToHoldDownMouseBeforeSpawn)
{
timer = 0.0f;
var crate = Instantiate(crystal, ray.origin, Quaternion.identity);
}
There are a lot of error in this code, I am not sure if you put it on purpose or not to check if I know what I am doing. I got it working but how will I work it out so it will spawn where I want it to.
well, you just change the second variable in Instantiate to where you wanna spawn it. About the errors, im sorry, i rely too much on intellisence for the spelling.