• 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 Nemox · Dec 03, 2012 at 04:27 PM · collisionraycastcharacternormalground

What's a good way to find the ground normal?

I'm gonna be kinda abstract about this question this time.

What's a good way to find the normal of the ground a character is standing on?

Comment
Add comment · Show 5
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 AlucardJay · Dec 03, 2012 at 04:34 PM 0
Share

Check Aldo's answer to this :

http://answers.unity3d.com/questions/179095/how-to-make-a-enemy-bleed.html#answer-179132

basically the .normal

avatar image Nemox · Dec 03, 2012 at 04:43 PM 0
Share

But the .normal of which collision? A character could be touching multiple surfaces at a given moment.

avatar image whydoidoit · Dec 03, 2012 at 05:00 PM 0
Share

So ray cast from the position of the character in -transform.up and get the normal from the raycast hit... Or -Vector3.up and get the pure normal of that location.

avatar image Nemox · Dec 03, 2012 at 05:58 PM 0
Share

How far would one have to raycast? Depending on the slopes, a downward raycast would have to extend very far. Then in cases of flat surfaces, it would detect ground far below the character.

avatar image sirival · Dec 03, 2012 at 10:41 PM 0
Share

You can ray cast using an infinite distance but use a layer mask to avoid raycasting a lot of objects. Perhaps put your terrain in a Terrain layer and just look for that in Vector3.down like whydoidoit said

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 04, 2012 at 01:16 AM

You could cast a ray in the -transform.up direction or in the Vector3.down direction, as @whydoidoit said. Vector3.down gives the normal of the surface below the character position, while -transform.up returns the normal under the character feet. The raycast range may be a little longer than the distance from the character pivot to its feet in order to compensate for inclined surfaces. You must decide what to do when the ground is out of the raycast range: you can keep the last surface normal or assume the world up direction (Vector3.up).
This is a simple code to find the normal under the character feet; if no ground is hit, the last normal is kept:

 private var distToGround: float;
 private var normal: Vector3 = Vector3.up;
 
 function Start(){
   distToGround = collider.bounds.extents.y - collider.center.y;
 }
 
 function Update(){
   // assume a range = distToGround + 0.2
   if (Physics.Raycast(transform.position, -transform.up, hit, distToGround + 0.2)){
     normal = hit.normal;
   }
 }

If you want to use the ground normal to make a character that aligns to the ground, take a look at the question Orient vehicle to ground normal. If you want a character that walks on any surface, take a look at the question Basic Movement. Walking on Walls.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Cylinder Ground Collision 0 Answers

How do I use RayCast in a 2D Platformer game? 1 Answer

Collision normal angles. 1 Answer

How can i detect the normal of the collider the Character Controller is touching. 2 Answers

Character controller model colliding. How to disable it? 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