Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SmittyA · Jan 24, 2021 at 11:31 PM · collisionraycasthitboxcollidernormalcast

Why are RaycastHit surface normals of corners not interpolated or otherwise correct?

I am using a BoxCast to test a (custom) kinematic character controller moving through the scene before actually updating the character's transform. Using a traditional collide and slide algorithm, I project the velocity along the RaycastHit's surface normal if I hit something, allowing me to slide along angled surfaces, like the cylinder in the below images.

alt text

In the first image, the yellow lines indicate where collisions are detected but are correctly projected. Red lines are where collisions are detected but fail to project (but these red lines in the first image are expected, as these are auto-step tests). The red lines all properly indicate the surface normal, which is easier to see on the lines at the bottom of the mesh.

alt text

In the second image, the red ray is pointed in the opposite direction (-Z) of my character movement (+Z). This surface normal results in a projection that puts the character controller moving in the opposite direction, cancelling out the movement.

Why would this surface normal be pointed in this direction, and not simply be an interpolation of the two neighbouring surfaces? Is this expected behaviour, and if so, how does one work around something like this to correctly round a corner? While I cannot use a capsule collider for this character controller (due to the cons of a rounded bottom outweighing the pros of a rounded body), I tested this with a capsule anyway and still get what look like incorrect normals, so I can rule out the type of collider being an issue. Any help at this point would be greatly appreciated! Thanks.

screenshot-2021-01-24-191106.png (198.9 kB)
screenshot-2021-01-24-191100.png (196.0 kB)
Comment
Add comment · Show 1
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 SmittyA · Jan 24, 2021 at 11:40 PM 0
Share

I'd also like to add that the surface normal being returned on the corner is always the inverse of the character's movement. The image below shows the normal along the -X axis, with the character moving into it along the +X axis. alt text

screenshot-2021-01-24-193821.png (175.7 kB)

1 Reply

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

Answer by SmittyA · Jan 31, 2021 at 06:53 PM

I love having to answer my own obscure questions. I still don't know why this happens, but I have a workaround that is holding up so far, so I hope this helps someone else looking for this.

When I hit something in a BoxCast, I check the surface normal. If the dot product between my movement's direction and the surface normal is close to -1, I can assume I'm either walking into a flat wall, or, a corner. In my project, I separate horizontal movement from vertical, so I only check this when moving in my "side pass" Either way, I perform a single Raycast (Collider.Raycast, so I'm only checking against that one collider). If I hit the obstacle again, I update my previous collision's surface normal with the "true" surface normal, because Raycast doesn't seem to have this issue, only box/capsule casts.

This may not catch every possible case, but so far so good for me! Hope this helps.

 // In some cases, walking into a corner can result in a normal that is the inverse of the desired movement
 if (pass == EPass.SIDE)
 {
    float threshold = -0.98f;
    if (Vector3.Dot(direction, closestHit.normal) <= threshold)
    {
        // Raycast from the volume's center, into the collision point we know we hit before
        Vector3 rayOrigin = m_Collider.transform.position + m_Collider.center;
        Vector3 rayDirection = (closestHit.point - rayOrigin).normalized;
        float rayDistance = Vector3.Distance(closestHit.point, rayOrigin) + m_Params.ContactOffset;
        Ray ray = new Ray(rayOrigin, rayDirection);
  
        // If we were able to hit the collider, grab the "true" surface normal so the volume can project collisions correctly
        if (closestHit.collider.Raycast(ray, out RaycastHit hitInfo, Vector3.Distance(closestHit.point, rayOrigin) + Physics.defaultContactOffset))
        {
            closestHit.normal = hitInfo.normal;
        }
    }
 }
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.

Follow this Question

Answers Answers and Comments

176 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 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

Not detecting a simple collision? 2 Answers

Box Colliders Merging 0 Answers

empty object with player controller attached isnt detecting collisions 2 Answers

Collision Detection for Kinematic Rigidbodies 0 Answers

Collisions, Getting the Normal of the Collision Surface (Not the Angle of Impact) 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