- Home /
Rotate character in 3D platformer
Hey guys,
so I'm building a 3D (what I guess is 2.5D really) platformer but having a bit of trouble with the character control.
I am using basic shapes at the moment, so a capsule with a character controller on it, and some cubes for the level. I can move left and right, and jump just fine so the actual movement is not at all an issue. What I am unable to figure out (and may not be possible) is rotating the capsule (to be replaced with a model later) to look in the direction he's going. I tried googling, other answers on here, and youtube and can't seem to find one specific to my issue.
Here is what I have so far.
public class PlayerController : MonoBehaviour {
[Range(1.0f, 10.0f)]
public float movementSpeed = 1.0f;
[Range(1.0f, 15.0f)]
public float jumpSpeed = 8.0f;
[Range(1.0f, 30.0f)]
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
// if we are on the ground
if(controller.isGrounded)
{
// we are on the ground so calculate the direction
moveDirection = new Vector3(Input.GetAxis("Horizontal") * movementSpeed, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
// jump
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
// apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// move
controller.Move (moveDirection * Time.deltaTime);
// kill us at the bottom...
if(transform.position.y <= -6)
transform.position = Vector3.zero;
}
}
I have tried a few different ways to change the character's rotation but none seem to work. I've used Quaternion.Slerp to the new rotation based off the moveDirection. I've tried altering the transform.rotation and eularAngles and just flat Transform.Rotate but they seem to all mess with the movement of the character controller.
My desired effect is that of a normal 2D platformer but in 3D (I also tried to look at the example given with Unity 5 in 2D but it just flips the scale which didn't work either). I want to be able to change the direction the character is facing but also make sure that the direction of the input doesn't change.
If I try to manually change the rotation in the inspector, the "d" key would normally make me go forward, now I travel backwards with the different rotation.
Thanks for the help guys.
Answer by Narv · Jun 01, 2015 at 04:14 PM
Found this question that had the correct answer:
http://answers.unity3d.com/questions/28109/how-to-turn-and-face-the-direction-im-moving-using.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613200437im_/https://answers.unity.com/themes/thub/images/avi.jpg)