- Home /
Trigger Event not Working Properly
I'm having a problem with getting a trigger event to work. I'm trying to get it so that when an enemy object collides with an object with the tag "Player" that it destroys the object. Except the event is only being triggered when the player object is falling down while it is in the air.
I made sure that the player object is a non kinematic rigidbody and the enemy object is set up as a trigger but I don't know if there is a problem with the objects or with the code itself.
Here is the code I'm using for enemy behavior:
function OnTriggerEnter( other : Collider)
{
if (other.tag == "Player")
{
Destroy(other.gameObject);
}
}
Answer by TimXT · Sep 29, 2013 at 11:01 AM
Posting your code would help see if there's something not right.
Other than that another problem might be that the player is already inside the trigger in which case it won't work.
Trigger events fire only when a target enters the trigger, and won't work if its already inside it. Maybe that's why when your player is falling down into the trigger for the first time it's working.
I edited my post with the code I'm using. The player object remains stationary while the enemies are instantiated and moved on screen towards it.
Hmmm, your code seems to be perfectly fine so it's definitely something else. How are you moving your enemies btw? Remember that triggers don't work if you move an object directly using the transform.position component. If you are doing that, then you must add a rigid body component to the moving object (i.e. your enemies).
That was it. I had the rigidbody on the player but not on the enemy. Added that on and it's now working, thanks.
$$anonymous$$oving triggers must have a rigidbody, usually set to kinematic.