- Home /
problem with instantiate
Hey guys, i'm making a 2D game but i'have problem with my enemy shoot, when he instantiate a bullet, he instantiate many bullet but i would like one by one, this is the code of the enemy :
function OnTriggerStay(col : Collider) { if(col.gameObject.tag == "Player") { Instantiate(lazerBullet, shooter.transform.position, lazerBullet.transform.rotation); } }
and the code of the bullet :
//var perso : Transform;
var shootSpeed : int;
function Start ()
{
}
function Update ()
{
var shoot = Time.deltaTime * shootSpeed;
transform.Translate(0,-shoot,0);
}
Answer by Leuthil · Mar 28, 2014 at 06:33 PM
Perhaps try this?
var shootSpeed : int;
// Set in Editor to be how many shots are fired per second
var shotsPerSecond : float;
private var shotTimer : float;
private var shotTimerElapsed : float;
function Start ()
{
shotTimer = 1 / shotsPerSecond;
shotTimerElapsed = 0;
}
function Update ()
{
shotTimerElapsed = shotTimerElapsed + Time.deltaTime;
}
function OnTriggerStay(col : Collider)
{
if (col.gameObject.tag == "Player") {
if (shotTimerElapsed >= shotTimer)
{
shotTimerElapsed = 0;
Instantiate(lazerBullet, shooter.transform.position, lazerBullet.transform.rotation);
}
}
}
You don't really want to be increasing a timer in a collision or trigger event since it can happen multiple times in the same frame depending on how many things are colliding in that frame.
Good point, didn't think about the "multiple times in the same frame" part. Not used to doing it that way :)
Answer by JasonBricco · Mar 28, 2014 at 06:15 PM
OnTriggerStay will repeatedly activate as long as the collider is touching. You could add in a delay, such as:
var delay = 0.0;
var delayAmount = 1.0;
function OnTriggerStay(col : Collider)
{
if (col.gameObject.tag == "Player" && Time.time > delay)
{
delay = Time.time + delayAmount;
Instantiate(lazerBullet, shooter.transform.position, lazerBullet.transform.rotation);
}
}
That way, you'll force it to wait (in this case) a second before firing again.
thak's JasonBricco, but it works just for the delay but it always instantiate a lot of bullet in the same time :(
That's odd... because it would only ever run the Instantiate function if the current time is greater than the delay time (and it should only run it once, because it immediately modifies the delay time to add some extra time to it).
Are you saying one instantiate call makes multiple bullets, or that instantiate is being run multiple times when it shouldn't be?
when the instantiate function is called, it's instantiate multiplayer of bullet, and i tryed to change the instantiated object and it's doing the same sh... lol
So even if you put this Instantiate() call in a start or awake function it would make multiple of the object? Sorry, I'm just trying to understand exactly what's wrong. Seems like an odd issue to me.
maybe it was instantiate since de character enter in the trigger zone and as there were no shoot per second, maybe that was the problem
Answer by alexizz · Mar 28, 2014 at 06:44 PM
it WORK's, thank you so much dude and sorry if my english is not well, i'm French lol
Your answer

Follow this Question
Related Questions
Instantiate at Location Problem 2 Answers
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Get Component on Instantiated Object 1 Answer
Instantiate problem 1 Answer
Instantiate A Prefab 1 Answer