Mesh Collider

Hi,

I’m getting crazy with a mesh collider… I need to check wether an object is inside the cone vision or not. I use OnTriggerEnter and OnTriggerExit for this.
The “funny” stuff is that if the player goes in the vision cone by the bottom of the pyramid, I have both OnTriggerEnter and OnTriggerExit which fires.

I tried to use OnTriggerStay, and it’s even weirder: OnTriggerStay works only when I go outside of the mesh.

I tried to check the concave option, but the only thing it does is that OnTriggerExit is never called then…

My player character has a Character Controller and a Rigidbody, and the pyramid has a Rigidbody as well.

Any idea how I can make it work? (with a Box Collider it works just fine… but I need a cone/pyramid :frowning: )

[20191-capture+d’écran+2014-01-04+22.29.58.png|20191]

I had the same problem once. The mesh colliders are a little weird. At least i couldnt make it work.

You dont need a mesh for enemy sight though. You could through script instead just check if player is within range, if he is within a certain angle in front of the enemy, and if you can raycast to him. If you want, instead of checking if he is within range, you could change your view cone to a box collider, and just make sure “not to see the player” if he is not within the angles.

var fieldOfViewAngle : float = 150f; // the angle of sight, 150 degrees
var Dist : float = 30.0; // the max distance of sight
    
var direction : Vector3 = Player.position - EnemyEyes.position; // Get the direction between the player and the eyes of the enemy
var angle : float = Vector3.Angle(direction, Enemy.forward);
	    
// if player is within the right angle
if(angle < fieldOfViewAngle * 0.5f)
{
   var hit : RaycastHit;
   
   // Draw a ray if you want
   Debug.DrawRay(EnemyEyes.position, direction);
	
   // make a raycast. Using Dist also makes sure player is within range.        
   if(Physics.Raycast(EyeSpot.position, direction, hit, Dist)) // You may also want to add a layermask to this raycast if you have seethrough objects
   {
       // If theres nothing between the enemy and the player
       if(hit.collider.transform == Player.transform)
       {
            // Enemy can see player
	            	
	}
}

You need to check what is entering the collider.

function OnTriggerEnter(theCollider : Colldier)
{
     if(theColldier.tag != "Player")
     {
           //Whatever you want
     }

}

You will have to make the tag for the player in the inspector, add the same if statement to the OnTriggerExit.

Hi,

I already have that.

In fact, after digging a bit more: when my Player is not moving, OnTriggerEnter works but not OnTriggerExit on certain sides of the pyramid. It works on some of them, but not on others. When the Player is moving however OnTriggerExit works on every sides…

Any idea?