- Home /
How to rotate CharacterController on slope
I'm trying to rotate my characterController on a slope on the 'x' and 'z' axises, however things aren't going so well.
The character does rotate, but if I move and look in another direction, the characterController will still be on the same axis, and won't look down an up slope, and instead will be looking up a down slope.
void Update () {
Vector3 rotateDirection = (camera.forward * Input.GetAxis("Vertical")) + (camera.right * Input.GetAxis("Horizontal"));
Quaternion newRotation = Quaternion.LookRotation(new Vector3(rotateDirection.x, 0f, rotateDirection.z));
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, rotSpeed * Time.deltaTime);
}
private void OnControllerColliderHit(ControllerColliderHit hit) {
transform.rotation = Quaternion.Euler(hit.transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, hit.transform.rotation.eulerAngles.z);
}
Also, it doesn't work at all with spheres and terrain since their rotation is zero. Anyone know how to achieve what I am talking about? Also, the goal is to allow for the 'y' axis to be free and do what it needs to in void Update()
Thanks in advance!
are you using the default CharacterController code for character coding, or is this a custom script?
ok, so the default character controller WILL not allow you to change your rotation according to the slope. You may have to create a original controller that uses colliders to rotate normally... you could find many examples that should fit your needs
Huh? No, the character IS rotating, just not properly, if I am on a slope that is rotated upward, the character will look up on the slope, but if the player looks in the opposite direction, the player is still looking up.
Your answer
