x


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

more ▼

asked Jan 21 '10 at 04:42 AM

Daniel 1 gravatar image

Daniel 1
31 8 8 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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).

more ▼

answered Jan 21 '10 at 05:37 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Thanks, using trigger worked better :)

I ended up using this code:

[code]

var int_HitRadiusDistance = 20;

function OnTriggerEnter (other : Collider) {

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

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

        // DEBUG/TEST: Remove this
		transform.position = Vector3(100, 200, 10);
    }
}

} [/code]

I will probably exchange the check for FPS and Player with a trigger object, so I can just drag the player-object to the script.

I tried putting it on a cylinder, which had the following characteristics: 1) Transform (of course), 2) Mesh Filter, 3) Collider (Is Trigger), 4) Rigid body (not use gravity)

The player should have a collider as well.

Well, thats about it - now for the attack-code :P

Jan 22 '10 at 02:56 AM Daniel 1

Thanks for posting your findings Daniel - and thanks for your answer Eric5h5 - between the two of you I was pointed in the right direction to fix an issue that's been hounding me for months... Much thanks!

Apr 01 '11 at 02:30 PM Brian 7
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x2000
x1705
x1535

asked: Jan 21 '10 at 04:42 AM

Seen: 2956 times

Last Updated: Jan 21 '10 at 09:58 AM