- Home /
Hit detection at angles of enemy character controller?
I'm trying to get some raycast and/or collision detection working.
If my enemy gets hit by a car on his left side, an animation will play of him tumbling away to the right. I'm not using physics for the collision, the animation for each direction is already finished.
I'm hoping to develope this enemy script to detect both raycast hits (machine gun) and collider hits (rocket, car, etc) and will share the end results.
The script needs to to figure the angle of hit on the controller and fire the script. I'm thinking something along lines of:
if(hit.angle>180) animation.Play("tumbleLeft");
if(hit.angle<180) animation.Play("tumbleRight");
Seems the class "ControllerColliderHit" would get me started but still having trouble getting it together.
Any info is greatly appreciated.
Answer by Molix · May 04, 2010 at 04:56 PM
You could use a Dot product of the hit direction and the left or right facing of the player. The dot product is > 0 if they're +/-90 degrees.
void OnCollisionEnter( Collision collisionInfo )
{
Vector3 hitDir = collisionInfo.contacts[0].point - transform.position;
Vector3 left = transform.TransformDirection( Vector3.left );
if( Vector3.Dot( left, hitDir ) > 0 )
{
Debug.Log("Hit from Left");
}
else
{
Debug.Log("Hit from right");
}
}
Edit: an attempt at a JS version:
function OnCollisionEnter( collisionInfo : Collision )
{
var hitDir : Vector3 = collisionInfo.contacts[0].point - transform.position;
var left : Vector3 = transform.TransformDirection( Vector3.left );
if( Vector3.Dot( left, hitDir ) > 0 )
{
Debug.Log("Hit from Left");
}
else
{
Debug.Log("Hit from right");
}
}
The OnCollisionEnter event in the docs is here.
Thanks for the info $$anonymous$$olix! I'm still trying to get it working but this is definately helping me along. Where am I getting the "collisionInfo" variable? Or is there a missing colon in the first line? I'm trying a few things but still not working. Thanks again!
Oh, the code is C# (hence the lack of a colon). The collisionInfo is passed to the OnCollisionEnter event of the scripts. I'll edit above with JavaScript version in a moment.
Great, I got it working! $$anonymous$$y fault for not noting that I'm using JavaScript. *Note for others: this will not work for raycasting (unless you know of a different way). As for OnCollisionEvent, Unity docs note "collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." (Rocket, etc.) I was having some confusion as I thought this would work with the raycast machinegun. Thanks a million for clearing this up for me $$anonymous$$olix, you rock!
I'm not too familiar with that tutorial, but it looks it does the hitting with RayCasts and not collision hooks. The example I wrote was assu$$anonymous$$g the script's object was getting hit by something physical and it wanted to know where on itself.
In the RayCast case you could use the 'hit' object (e.g. in the machine gun fire function), and then calculate the direction on the target. e.g. var hitDir : Vector3 = hit.point - hit.transform.position; var left : Vector3 = hit.transform.TransformDirection( Vector3.left );
Your answer
Follow this Question
Related Questions
Get angle from impact 2 Answers
Collision Not Responding 1 Answer
Is there a way to create an overlap custom-shape? 0 Answers