OnTriggerEnter sometimes does't work??

I have made a enemyAI when it collide the wall and it will turn around a random range, and it properly can turn around, but sometimes it will go through the wall,the enemy had add the rigidbody and box collider and check the kinematic on, the wall have collider too. Can somebody help me for this sulotion. thx

here is my code:
void OnTriggerEnter(Collider other)
{

	if (other.tag == "wall") {
		
		TurnAround();
		
		
	}
}



void TurnAround(){
	transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y-Random.Range (120F,240F) , 0);
}

Are you sure that the thing you want to turn has the tag wall?

Have you set is trigger true. ?

yes, l have tag the wall with those gameobject, and I also check the is trigger on, it is work almost everytime, but it sometimes will go through the wall, may be it happen once in 20 times collide the wall.

2 Answers

2

it is work almost everytime, but it sometimes will go through the wall, may be it happen once in 20 times collide the wall.

It seems that you are rotating the wall that is also used to detect the collision, using the Transform methods instead of Rigidbody. I think that will cause problems due to static colliders:

When using physics remember to attach a Rigidbody to the transformed objects, transform them only with Rigidbody operations, and do it always inside the FixedUpdate method.

thank you very much,but I have some problem with the rigidbody operations, Before I used transform translate to move the enemy, and the enemy can follow the random rotation to move. like this: transform.Translate(Vector3.forward * 8 * Time.deltaTime); now I change it to : rigidbody.AddForce(Vector3.forward * 8 * Time.deltaTime); now the enemy just move to aixs Z. so is it something wrong with the rigidbody.AddForce?

I’m having this same issue, when the fps is low OnTriggerEnter sometimes won’t get called, Odds are that my object was moving too fast for the trigger to be calculated, effectively being positioned over the platform one frame and below it the next.

You could do 1 of 2 things.

  1. Clamp the downward/y movement of the player character so it can only move so quickly

  2. Since the triggers use the physics update you could increase the frequency of the fixed timestep Unity - Manual: Time