Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by NecrosDk · Oct 28, 2012 at 05:10 PM · collisiondirectionangleenter

Detecting when an object enters a collider from the top?

My character with character controller rolls around, and sometimes hits my object which has a box collider. I want to do so that if i hit the object from above, the object plays one animation, and if the object is hit from any other side, it should play another. I know how to do the animation part, so all I need help with is how to detect where the collision happens.

Any ideas how to go about this one?

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
1
Best Answer

Answer by aldonaletto · Oct 28, 2012 at 05:32 PM

You could use OnControllerColliderHit in the character script, and check the hit normal: if it's almost vertical, you're colliding in the top-down direction. But there's an issue: this event happens all the time due to collisions with the ground, thus you should filter out the undesired cases checking if the collided object has the right tag. Another problem: the animation code probably is attached to the hit object, not to the character, thus you should pass a hint from the character script to the animation code.
An example: the animation script (let's call it AnimScript.js) could contain a function like this:

 function PlayAnimation(normal: Vector3){
   if (normal.y > 0.9){ // if hit normal angle from horizontal > 64 degrees (sin(64)=0.9)
     animation.CrossFade("TopDownHit"); // play top-down animation
   } else {
     animation.CrossFade("OtherHit"); // play other animation
   }
 }

And in the character script you could have a code like this:

 function OnControllerColliderHit(hit: ControllerColliderHit){
   if (hit.transform.tag == "Enemy"){ // supposing the other object is tagged Enemy
     // try to get its script AnimScript.js
     var anim: AnimScript = hit.transform.GetComponent(AnimScript);
     // if script got, call PlayAnimation passing the hit normal:
     if (anim) anim.PlayAnimation(hit.normal);
   }
 }

Comment
Add comment · Show 3 · 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 NecrosDk · Oct 28, 2012 at 05:55 PM 0
Share

Okay so I understand how to check for tags and send messages between objects etc.

I now understand that you can check for the hit normal, but I don't quite understand THAT. How does that work in more detail, if you would be so kind to explain? :D

EDIT: Thanks for taking your time to help of course! :)

avatar image NecrosDk · Oct 28, 2012 at 06:04 PM 0
Share

Alright so in the script reference I found the bit of code you use to recieve the normal which you collide at. One problem though; I simply use Send$$anonymous$$essage, to tell my object i hit to play an animation, but I cannot use Send$$anonymous$$essage if the object i collide with is not set to Is Trigger! The other way around, I cannot get the hit normal if my object IS trigger... What do I do here?

EDIT: I just wrote the code wrong, I can use sendmessage just fine. Problem solved!

avatar image aldonaletto · Oct 28, 2012 at 10:18 PM 0
Share

Send$$anonymous$$essage is a good alternative - a little slower than a direct call, but more versatile: since the function is called by name, the character script doesn't have to know the target script at compile time.
About the normal: being a normalized vector, its Y component is numerically equal to the sin of the elevation angle (angle relative to the horizontal plane XZ). If normal.y is higher than 0.9 (sin of 64 degrees), we know that the hit surface is almost horizontal (inside +/- 26 degrees range) and facing up.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate at a right angle from collision impact 1 Answer

Setting rotation angle of a bullet fired towards the mouse 1 Answer

Change Direction on Collision 6 Answers

How to check direction of wall on character controller hit? 2 Answers

On Collision Enter Press F 2 Answers


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