Physic Prevent rigidbody sliding unity

Rigidbody ourRigidBody;
bool isGrounded; // Handy to have for jumping, and we'll use it here too
Vector3 currentGravity; // Just holds some data for us...
ContactPoint[] cPoints; // ContactPoints are generated by Collision, and they hold lots of fun data.
public float maxGroundedAngle = 45f; //The steepest you want the character to be able to stand firmly on. Steeper than this and they'll slide.
Vector3 groundNormal; //The angle that will be perpendicular to the point of contact that our Rigidbody will be grounded on.

void Start()
{
    ourRigidBody = GetComponent<Rigidbody>();
    ourRigidBody.useGravity = false; //we'll make our own!
    /*Freezing rotation is not necessary, but highly recommended
      if we're making a character rather than just some object
      that happens to be on a slope.*/
}

void OnCollisionStay(Collision ourCollision)
{
    isGrounded = CheckGrounded(ourCollision);
}

void OnCollisionExit(Collision ourCollision)
{
    /*It's okay to not have to check whether or not
     the Collision we're exiting is one we're grounded on,
     because it'll be reaffirmed next time OnCollisionStay runs.*/
    isGrounded = false;
    groundNormal = new Vector3(); //Probably not necessary, but a good habit, in my opinion
}

bool CheckGrounded(Collision newCol)
{
    cPoints = new ContactPoint[newCol.contactCount];
    newCol.GetContacts(cPoints);
    foreach(ContactPoint cP in cPoints)
    {
        /*If the difference in angle between the direction of gravity
          (usually, downward) and the current surface contacted is
          less than our chosen maximum angle, we've found an
          acceptable place to be grounded.*/
        if(maxGroundedAngle > Vector3.Angle(cP.normal, -Physics.gravity.normalized))
        {
            groundNormal = cP.normal;
            return true;
        }
    }
}

void ObeyGravity()
{
    if(isGrounded == false)
    {
        //normal gravity, active when not grounded.
        currentGravity = Physics.gravity;
    }
    else if(isGrounded == true)
    {
        /*Not normal gravity. Instead of going down, we go in the
        direction perpendicular to the angle of where we're standing. 
        This means whatever surface we're grounded on will be 
        effectively the same as standing on a perfectly horizontal 
        surface. Ergo, no sliding will occur. */
        currentGravity = -groundNormal * Physics.gravity.magnitude;
    }
    ourRigidBody.AddForce(currentGravity, ForceMode.Force);
}

void FixedUpdate()
{
    ObeyGravity();
}

```

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source