- Home /
OnTriggerStay doesnt work unless moving?
I asked this question previously, but i guess i didnt word it properly so people didnt really understand what i am trying to ask. I have an enemy which has a sword. He is programmed to approach to player and play and animaton which swings the sword at the player and it collides with it. For some reason when he approaches the player and swings once, the collider detects the collision and i have it print out the collided object's tag which is always the player, but when he swings again and the player still hasnt moved, it doesnt detect a collision. Some other strange things is that if the player constantly moves into a wall or into the enemy, it works properly, as if the collision is only detected when the player is moving. Heres a picture of the enemies components and settings 
As you can see the collider looks like the size and shape it should be and the box collider is set to Trigger. here is my code for the script jumpdetect
public class jumpdetect : MonoBehaviour {
void OnTriggerStay(Collider c){
Debug.Log(c.tag);
}
}
So just to repeat myself.... when the enemy approaches and swings the sword (Which obviously collided with the player every time) it prints out about 25 lines of "Player" like it should... but next time around if i havent moved from the last time he hit me then it doestn print anything out. Ive been stuck on this issue for about 2 days now and im totally baffled by what the issue is. Thanks in advance for the help.
Collider are weird and don't like when they enter something else.
Think of it this way.
If you broke into someones house you would not call the police(function). If someone broke into your house you would call the police(function).
If your target is not moving it will not be called. I would suggest just using a raycast.
Answer by TimBorquez · Dec 16, 2013 at 09:34 PM
if you're using the character controller the collision functions don't seem to work all the time unless you're moving
you should be able to either:
-detect it from the character controller with this
-put a rigidbody on the sword because it's moving
-cheat and "move" the player constantly:
//put this code on the character controller
function Update () {
//add nothing to position yo
transform.position += Vector3.zero;
}
Im not using the character controller but i added a rigidbody to the sword and it works perfectly now! Thanks!
To clarify a wee bit, transform.position += Vector3.zero probably won't do the trick. Functions like OnControllerColliderHit() depend on calls to Simple$$anonymous$$ove() or $$anonymous$$ove().
controller.Simple$$anonymous$$ove(Vector3.zero); is the right "cheat" you're looking for.
Your answer