Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[Tutorial] Cancel rigidbody slipping on a slope

Discussion in 'Community Learning & Teaching' started by TheMaster_123, Dec 20, 2021.

  1. TheMaster_123

    TheMaster_123

    Joined:
    Feb 26, 2019
    Posts:
    1
    I have been trying to stop a Rigidbody from slipping on a slope(with a frictionless surface), and I think figured it out.
    So I thought this would be useful to share.
    Here is my code.
    Code (CSharp):
    1. if (!onGround) return;
    2.  
    3. Vector3 slipDirection = (contactNormal - -Physics.gravity.normalized);
    4. Vector3 slipVelocity = slipDirection  * Physics.gravity.magnitude;
    5.  
    6. rigidBody.AddForce(-slipVelocity, ForceMode.Acceleration);
    I hope you find this useful.
     
    MysteriousType likes this.
  2. MysteriousType

    MysteriousType

    Joined:
    Nov 17, 2020
    Posts:
    1
    You saved my day!
    For more information: you can get
    contactNormal
    from this raycast:
    Code (CSharp):
    1. Physics.Raycast(transform.position, Vector3.down, out RaycastHit groundHit, distance, _groundMask);
    2. Vector3 contactNormal = groundHit.normal;