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

Getting a CapsuleCollider to move on terrain without sliding

Discussion in 'Physics' started by shakozzz, Apr 28, 2017.

  1. shakozzz

    shakozzz

    Joined:
    Mar 1, 2017
    Posts:
    60
    Hi guys,
    I have a CapsuleCollider with a Rigidbody component and a camera as a child object which acts as my player. I'd like to be able to move it across a hilly terrain i.e. being able to go uphill, but not sliding downhill when I'm not moving, while also being able to collide with trees and of course not sinking through the terrain. I'm currently using transform.translate() to move around. I tried adding a physics material with high friction, but it had very little effect. Making it Kinematic was of course a disaster. The closest thing I've had to success so far was freezing the player's position if I'm not pressing any movement buttons, but the player takes like half a second to freeze so slides a tiny bit before stopping. Nothing on the forums seems to solve this problem conclusively. The FPSController from the asset store doesn't have this issue, but it uses CharacterController.Move() to move the player and adds a downward force of -10 if the player is grounded. I tried adding a downward force of my own to no avail. Any tips?
     
  2. shakozzz

    shakozzz

    Joined:
    Mar 1, 2017
    Posts:
    60
    So, what I ended up doing was I used a Raycast projected downward from my capsule/player and disabled gravity on the Rigidbody so that the capsule is essentially always floating a certain distance from the ground. When the ray hits the terrain below me, it tells me the distance between my player and the ground directly beneath him. If the distance is greater than my preferred elevation, I subtract the preferred elevation from the distance, multiply by -1 and the result is my new y parameter of the translate, essentially moving the player down a little till the distance between him and the terrain is my preferred elevation. If, however, the distance is less than the preferred elevation, then the distance is subtracted from the preferred elevation and that becomes the new y parameter of the translate, essentially moving the player up a bit to the preferred elevation.
     
    Last edited: Apr 28, 2017
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    this thread is about 2D but has some notes that can surely be used in 3D.
    Read and Scroll to see the opinions/ideas/tips offered :)
    https://forum.unity3d.com/threads/physic-prevent-rigidbody-sliding.341467/

    That is , unless it's solved to your satisfaction.
    One other note, is that it's generally not great to move a non-kinematic rigidbody around the world with translate. But it's up to you if it's working okay, I guess :)