- Home /
OnTriggerStay explanation?
I have created a tower defense style system, and for the most part, it works great. When objects enter the collisionbox of the turret, the turret attacks them until they. When they die, the turret stops firing.
I have noticed that there is some weirdness however, when 2 ore more enemies are in the collision box of the turret. If one dies, the turret fails to pick up on the other. And just stops firing all together.
I want to solve this with OnTriggerStay, but when I replace that function with my current OnTriggerEnter, the turret will not fire. If I just include an added OnTriggerStay function to work with my current OnTrigger Enter/Exit functions, again...the turret refuses to fire or detect enemies in it's collision box. So I'm clearly mis understanding how to use this function.
Here's my turret code;
var rangeX : float = 0.0;
var rangeY : float = 0.0;
var rangeZ : float = 0.0;
var projectile : GameObject;
var projectileSocket : Transform;
var myTarget : Transform;
var reloadTime : float = 1.0;
var nextFireTime : float = 0.0;
function Update ()
{
if ( myTarget )
{
if ( Time.time >= nextFireTime )
{
FireProjectile ();
}
}
}
function OnTriggerEnter ( other : Collider )
{
if ( other.gameObject.tag == "alien" )
{
nextFireTime = Time.time + ( reloadTime * .5 );
myTarget = other.gameObject.transform;
}
}
function OnTriggerExit ( other : Collider )
{
if ( other.gameObject.transform == myTarget )
{
myTarget = null;
}
}
function FireProjectile ()
{
audio.Play ();
nextFireTime = Time.time + reloadTime;
Instantiate(projectile, projectileSocket.position, projectileSocket.rotation);
}
function OnDrawGizmos ()
{
Gizmos.color = Color.white;
Gizmos.DrawWireCube ( transform.position, Vector3 ( rangeX, rangeY, rangeZ ) );
}
Answer by OP_toss · Aug 19, 2013 at 07:29 PM
I would keep using Enter and Exit. Then just keep a Queue of your targets, Enqueuing whenever one Enters, and Dequeueing when one Exits. Then when one dies, Dequeue, and move to the next target, if it exists. Perfect use for a Queue.
Hope this helps!
I'm not familiar with Queue, could you give me an example of how it's used? (I'm relatively new to Java)
It's just like a queue in real life. First-in, First-out. People join the line at the end, the person at the front gets their meal, and gets dequeued off the front of the queue. $$anonymous$$ake sense?
The actual API for Queue can be found in the documentation online. All you need to use is Enqueue (push/add), and Dequeue (pop/remove), and maybe Clear.
I've done a search within Unity's API and nothing comes up to what I would need. It looks to me like allot of animation functionality, but not a first in/ first out style type function.
Could you point me in the right direction or just give me a small sample code of how it works wihtin Unity?
http://msdn.microsoft.com/en-us/library/7977ey2c.aspx
It's not a Unity thing. It's a .NET C# thing.
Being new to coding in general, just reading a description of Queue isn't enough for me to figure out how to properly understand or use it. I'm just not there yet. I need something more then just the API. I've been going through as many online tutorials as I can get my hands on, but so far none of them have touched on queue.
Thankfully OnTriggerStay is perfect for my situation and all I need right now. I'll keep digging into queue though, for future use, as what you described does sound like it would be helpful down the road.
Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613112535im_/https://answers.unity.com/themes/thub/images/avi.jpg)