Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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;