- Home /
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
Answer by Eric5h5 · Jan 21, 2010 at 05:37 AM
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).
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) $$anonymous$$esh 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
Thanks for posting your findings $$anonymous$$ - 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... $$anonymous$$uch thanks!
Your answer
Follow this Question
Related Questions
Raycast 2D with origin inside the collider 1 Answer
Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
physics2d.raycastall help please 1 Answer
Swapping 2 objects positions when one is dragged into the other 1 Answer