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 zenmasterchris · Apr 10, 2020 at 05:24 AM · platformerraycasthitvector2ground detectionslopes

hit.normal != Vector2.up evaluates to true even when hit normal is Vector2.up

I'm trying to detect flat ground vs slopes in my 2D platformer. As a first pass I'm doing a Raycast2D but for one of my flat surfaces my code is registering a slope when the hit normal from the ray cast is returning the up vector! The upper flat part of the tilemap registers correctly, the slope also registers correctly, but the bottom left flat ground registers as a slope with a normal vector pointed up (see my debug log output in the attached screenshot)!

alt text

Here's my code:

 private bool IsGrounded()
     {
         float extraHeight = 0.6f;
         RaycastHit2D hit = Physics2D.Raycast(collider.bounds.center, Vector2.down, collider.bounds.extents.y + extraHeight, platformLayerMask);
         Debug.Log(hit.collider);
 
         Color rayColor = Color.red;
         if (hit.collider != null && hit.normal == Vector2.up)
         {
             Debug.Log("FLAT GROUND! hit.normal = " + hit.normal);
             rayColor = Color.green;
         }
         
         if (hit.collider != null && hit.normal != Vector2.up)
         {
             Debug.Log("SLOPE! hit.normal = " + hit.normal);
             rayColor = Color.blue;
         }
 
         Debug.DrawRay(hit.point, hit.normal, Color.white, 5.0f);
         Debug.DrawRay(collider.bounds.center, Vector2.down * (collider.bounds.extents.y + extraHeight), rayColor);
 
         return hit.collider != null;
     }


gamesnip.png (90.9 kB)
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

Answer by zenmasterchris · Apr 10, 2020 at 05:56 AM

I output the value of the hit normals using ToString("F8") because it turns out (thanks to this thread) the normal ToString() has low precision. The hit normal coming from the flat surface on the lower left is quite a bit further away from being exactly (0, 1) than the hit normal on the upper right flat surface.

Comment
Add comment · Show 1 · 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 Bunny83 · Apr 10, 2020 at 12:49 PM 0
Share

Right. That's a general issue with floating point values. Just to actually provide a solution: If you want to check if a direction is about the same direction you can use the Dot product like this:

 if (Vector3.Dot(hit.normal, Vector2.up) >= 0.99f)

or to have a better intuition for the threshold you can use Vector3.Angle

 if (Vector3.Angle(hit.normal, Vector2.up) < 0.1f)

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

130 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 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 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 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 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 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 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 to make my character stop stuttering when walking down a slope? 2 Answers

I'm creating a 3D platformer, but whenever my player goes down a slope, it thinks it isn't grounded, resulting in a weird animation glitch. 1 Answer

How to check if a raycasthit detects an angle less than 45 degrees and touches the ground 2 Answers

Having trouble with simple 2D movement 4 Answers

Preventing CharacterController sliding down edges on platforms, ground detection. 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