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 galhajaj · Dec 02, 2019 at 05:40 PM · rigidbodycollider3dvelocity

Slide when touching walls without losing velocity

For top-down 3d game with obstacles and walls around it (arena like) How to make the moving object not lose velocity when touching walls and obstacles? If for example, the object moves in an angle of 45 deg to the wall, it loses 50% from its movement speed (as it should in real life... but in my game I want it to behave differently) I want the object to continue moving along the wall without losing the 50% velocity

the moving object is with a rigid body and the obstacles and walls are colliders. I planted physics material with 0 friction to the moving object and the obstacles

Thanks

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
0

Answer by Ermiq · Dec 03, 2019 at 12:03 PM

Quite tricky thing.


First of all, you'll need to use Rigidbody's OnCollisionStay() event to check if collided with a wall (or something else that has its surface positioned at ~90 degrees to the ground).

To do that, you'll use the GetContacts property of the Collision, iterate through them and find out those contact points where the normal (a vector that is perpendicular to the surface) is at ~90 degrees to the ground normal. If Collision has such points, then it means you are touching a wall.

 ContactPoint[] points = new ContactPoint[20];
 Vector3 wallNormal; //this will store the vector that points out from the wall
 bool isTouchingWall;
 
 OnCollisionStay(Collision col)
 {
     int pointsAmount = col.GetContacts(points);
     isTouchingWall = false; //don't know if we're near a wall yet
     for (int i = 0; i < pointsAmount; i++)
     {
         //if angle between the touched surface and the plane ground is more than 80 degrees, than it means this surface is kind of a wall
         if (Vector3.Angle(points[i].normal, Vector3.up) > 80f)
         {
             wallNormal = points[i].normal;
             wallNormal.y = 0f; // eliminate all possible slopes of the wall touched so the vector will be ideally horizontal.
             isTouchingWall = true;
             break; //quit the iteration as we already got some wall
         }
     }
 }

So, now if the rigidbody has detected some wall, isTouchingWall will be true. But what if we jump? In this case, OnCollisionStay() won't be triggered, and isTouschingWall might stay true. Set it to false in:

 OnCollisionExit() {
     isTouchingWall = false;
 }


Now, we know when the rigidbody is near a wall, and we know the wall normal vector.

With this info you'll need to change your move direction that is applied to the rigidbody in your movement methods. The resulting direction should point along the wall surface. To get this new direction you'll need to use Vector3.Reflect to get the opposite version of the regular move direction (kind of reflected off the wall surface). And than multiply the regular move direction with the reflected direction to get the centered direction that is pointing in the middle of them, that will be the direction along the wall.

 void Move()
 {
     //you've calculated your move direction as usual
     moveDirection = ... // your code
     // and than you should change it so it will point not in the wall, but along the wall
     if (isTouchingWall)
     {
         //get the direction that will be your move direction reflected off the wall
         Vector3 reflectedDir = Vector3.Reflect(moveDirection, wallNormal);
         //and get the middle center direction between the two
         moveDirection *= reflectedDir;
         rb.AddForce(...); //your code to move the rigidbody
     }
 }
     
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

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

Inconsistent Jump height 3d 3 Answers

When I look around, my character rotates in a weird circle and it causes it to move around. 0 Answers

3D Model problems 1 Answer

How to rotate an object 360 degrees around a sphere with collisions? 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 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