• 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 · 4 days ago · 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 · 3 days ago

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Inconsistent Jump height 3d 3 Answers

Collider question 0 Answers

Calculate Angular Velocity to Match Remote Position 0 Answers

rigidbody velocity on x axis and addforce on y axis ?? 1 Answer

Multiplying something.forward using a variable doesn't actually multiply, but multiplying by a number does. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges