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
0
Question by Mysta · Feb 11, 2014 at 10:33 PM · collisiondirectionwallsoncontrollercolliderhit

How to check direction of wall on character controller hit?

Say you're in your game, running, and jump up in the air, and start sliding along a wall to your right. How would you tell which side of you the wall is on? So far I have this collision detected as shown below but stuck on this part. Been toying around with move direction and such. Is there a way to do it with components of controllercollider hit or should I send out 4 rays in each direction and see which hits first?(Would this be bad practice? Single/MP game?)

     void OnControllerColliderHit(ControllerColliderHit hit) {
         if (hit.collider.gameObject.tag == "Wall"){
             print("Yay WALL!");
         }
     }


Edit: I was able to resolve this by checking hit normal then comparing angle of the transform right(right/left) and forward(front/back) to see which of the 4 directions was facing the wall(45 degrees). Thank you for your help!

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

2 Replies

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

Answer by robertbu · Feb 11, 2014 at 10:40 PM

There are a couple of approaches to this problem. A character controller has a set of collision flags. The flags tell you what part(s) of the character were impacted. You can directly access these flags by CharacterController.collisionFlags. These flags are also returned by the CharacterController.Move() function. They don't tell you which side, just that the sides were impacted.

Either independently or in combination with the collision flags, you can do some vector math using the hit point from the ControllerColliderHit parameter. To figure out which side, start by calculating the direction of the hit from the pivot point:

 var dir = (hit.point - transform.position).normalized;

You can then get the angle between this vector and transform.forward, -transform.forward, transform.right and -transform.right. The side with the lowest angle is the one the hit is on. A slightly more efficient solution is to use Vector3.Dot() with these four directions.

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 Mysta · Feb 11, 2014 at 11:04 PM 0
Share

Hm, I was under the impression the flags only said "sides" ins$$anonymous$$d of which side?

avatar image robertbu · Feb 12, 2014 at 07:21 AM 0
Share

@$$anonymous$$ysta - you are absolutely right. I'd forgotten that it was only 'sides'. I rewrote my answer. Thanks.

avatar image Mysta · Feb 12, 2014 at 01:28 PM 0
Share

I'll give that a shot!

avatar image
1

Answer by Kavorka · Feb 11, 2014 at 11:24 PM

Check the normal vector. ControllerColliderHit.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 Mysta · Feb 12, 2014 at 01:28 PM 0
Share

Thanks! I'll compare that one against the above, my math is a bit rusty so I'm trying to think of ways to compare the way i'm facing to see which side is against the wall.

avatar image Kavorka · Feb 12, 2014 at 04:40 PM 1
Share

If vector3.Dot(transform.forward, ControllerColliderHit.normal) > 0 the character is looking away from wall otherwise the character is looking towards the wall. (Provided forward is forward)

avatar image Mysta · Feb 12, 2014 at 05:53 PM 0
Share

I cannot upvote yet but I used a combo of both of your answers to work my solution, thank you all!

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get the closest point on a concave mesh to a world position? 0 Answers

character collider colliding with another character collider 1 Answer

Moving a ball in the opposite direction of where it hits 1 Answer

Moving Randomly 1 Answer

handmade player object goes through walls 0 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