OnCollisionEnter-issue

Im trying to get an enemy to detect where the player is but at the same time restricting his vision to a cone from his eyes and forward.

The cone is an invisible object which should detect collisions using OnCollisionEnter.

When collision is detected, it should use a Raycast to see if the object is in sight (and not behind a wall that the cone is penetrating).

This is my code so far, and after messing with it for an hour or two now I cant figure out why the detection isnt made.

var int_HitRadiusDistance = 20;

function OnCollisionEnter( collision : Collision ) 
{
    var contact : ContactPoint = collision.contacts[0];

    if( ( contact.otherCollider.name == "FPS" ) || (  contact.otherCollider.name == "Player" ) ) 
    {   	
        var ray = new Ray( transform.position, transform.forward );         
        var hit : RaycastHit; 

        if( Physics.Raycast( ray, hit, int_HitRadiusDistance ) ) 
        { 
            // TODO: Attack-code.
        }
    }
}

The script is attached to the cone-object, which is a child of the enemy-object. For testing Ive used a unity-created cylinder instead of a cone, and unchecked Mesh Renderer. It will still prohibit me from moving trough it, though, so I havent solved that issue yet. How?

The player-objects name is "FPS".

Any suggestions?

Thanks, Daniel

You should use a trigger for the cone, and OnTriggerStay() for the raycast code. If the player is a CharacterController, it will also need a kinematic rigidbody attached with some kind of primitive collider in order to interact with the trigger (see the docs about colliders and interaction).