Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DigitalDogfight · May 04, 2010 at 12:26 AM · collisioncollideranglehit

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DigitalDogfight · May 05, 2010 at 12:35 AM 0
Share

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!

avatar image Molix · May 05, 2010 at 01:37 AM 0
Share

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.

avatar image DigitalDogfight · May 05, 2010 at 04:26 AM 0
Share

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!

avatar image Molix · May 05, 2010 at 04:44 AM 0
Share

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

avatar image Molix · May 05, 2010 at 04:47 AM 0
Share

Ah, I just saw your comment. Great!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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

Multiple colliders on a single character? 1 Answer

Disabling objects from Collision detection 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges