- Home /
Rigidbody controller and slopes
Hello! I made a rigidbody player controller for anti-gravity jumps and stuff, that's why i can't use character controller. Everything was fine, but then i bumped into slopes problem. Is there any way to move "through" slopes gracefully with rigidbody? As i said, i can't use character controller for my project.
Slopes or steps? Usually rigidbodies climb slopes, but get stuck at steps.
Answer by aldonaletto · Nov 23, 2012 at 01:45 PM
I used a capsule collider with a rigidbody character, and it could climb steps up to half its height. My script is as follows:
var horSpeed: float = 6.0;
var jumpSpeed: float = 8.0;
function Start(){
// no physics rotations, please!
rigidbody.freezeRotation = true;
}
function Update(){
// get the direction it must walk in:
var speed = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
// convert from local to world space and multiply by horizontal speed:
speed = horSpeed * transform.TransformDirection(speed);
// keep rigidbody vertical velocity to preserve gravity action:
speed.y = rigidbody.velocity.y;
// set new rigidbody velocity:
rigidbody.velocity = speed;
}
This is a basic movement script, and emulates the CharacterController behaviour - maybe it works as a starting point in your case. This script only moves forth/back and sideways - you can add a MouseLook script to the character with Axes = Mouse X to rotate it with the mouse, and another MouseLook to the camera (if it's a first person game) with Axes = Mouse Y to look up and down.
But I made once a simple "Gravity Guy"-like script using a CharacterController: it works in flipped gravity as well because the CharacterController is always vertical, even if we rotate the transform - the only problem is the property isGrounded, which will always be false when upside down.
If you want to walk on any surface instead - floor, ceil, walls etc. - use this script.