- Home /
C# OnTriggerStay Collider Problem
So I have a monster that has this script attached to it and I want it to move after it kills the cannon, but it just stands there:
void Start()
{
speed = walkingSpeed
}
void Update()
{
transform.Translate(Vector3.right * speed * Time.smoothDeltaTime);
}
void OnTriggerStay(Collider col)
{
if (col.tag == "cannon")
{
attackingTimer += Time.deltaTime;
speed = 0.0;
if (attackingTimer > attacksEvery)
{
col.SendMessage("CannonTakesAHit", iDamage);
attackingTimer = 0.0f;
}
}
}
So basically hits the cannon over and over, but when the cannon dies, it stands there doing nothing, and I want it to keep moving forward.
I tried messing around with OnTriggerExit, speed = walkingSpeed with no success, I also tried adding else if col.tag != "cannon" it works but it takes random amount of seconds before it starts moving... which is not what I am looking for unless I can make it where it waits exact amount of time, every time, before moving. I also tried adding a send message in the cannon script where it sends hey this cannon is dead, keep moving and it works, but only for 1 monster, so if I have lets say 3 monsters attacking the same cannon, and they kill it only 1 of them starts walking and the rest stay behind.
Answer by GoSuNeem · Oct 29, 2011 at 07:39 AM
Technically, as far as I know, the cannon never left the trigger.
Few things you can do here. Have a "dead" layer where if a object(in this case a cannon) dies move it to the dead layer so the monsters can't collide with the cannon anymore. (Make sure you set up your physics collision correctly! ;])
Or, on the line "if (col.tag == "cannon")" add a check to see if the object is still alive.
Those are the most simple/quick fixes, but there's a lot of other ways to correct this that's a lot more efficient.
Another thing I would like to point out is that make sure you set your physics collision correctly... I would think this piece of code would run into few problems such as it's colliding with another monster first so it can't collide with anything else.
http://unity3d.com/support/documentation/Components/Layer%20Based%20Collision%20detection.html
Hope that helps! Thanks.
Thanks, I am fairly new to Unity and I don't even know how to mess with layers properly so I rather not. I have been trying to figure out how to add a check to see if the object is still alive but unfortunately I don't know how to do that so I been trying different "jenky" things, but nothing has been working.
For changing the layer, all you have to do is get the gameObject then use .layer.
To set new layers, go to Edit>Project Settings>Tags. add in a new layer such as "$$anonymous$$onsters" "alive" and "dead". Remember that layer number! you'll need it later.
After, go to Edit>Project Settings>Physics.
Set it so monsters can't hit dead objects. and set your monsters and cannons to the correct layers (at the top right of your inspector)
when the cannon "dies" all you have to put is
gameObject.layer = *number of your dead layer object)
then you should all good to go.
The other way is to just set a bool on your cannon.
public bool alive = true; when it dies set the bool to false and in your monster code, add....
if (col.tag == "cannon") {
Cannon code name cannon = col.GetComponent<Cannon code name>(); if(cannon != null && cannon.alive) { attackingTimer += Time.deltaTime; speed = 0.0; if (attackingTimer > attacksEvery) { col.Send$$anonymous$$essage("CannonTakesAHit", iDamage); attackingTimer = 0.0f; } }
Sorry couldn't fit the rest in the last comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613024215im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I'm having a problem with OnTriggerStay2D 1 Answer
Distribute terrain in zones 3 Answers
Work around for onTriggerStay? 1 Answer
Multiple Cars not working 1 Answer
Call function on all objects in trigger 2 Answers