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.

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.


Hope that helps!