- Home /
CharacterController.move doesn't seem to move me horizontally
The following code is in a script that I'm calling ThirdPersonMovement. All I really want to accomplish is some simple character-controller based movement with a third person camera.
public float movementSpeed;
private CharacterController controller;
private Vector3 cameraForward;
private Vector3 cameraRight;
private Vector3 DirectonToMove;
private Transform mainCamTransform;
void Start ()
{
controller = gameObject.GetComponent<CharacterController>();
mainCamTransform = Camera.main.transform;
}
void Update ()
{
UpdateDirectionToMove();
transform.rotation = Quaternion.LookRotation(cameraForward);
controller.Move(DirectonToMove * movementSpeed * Time.deltaTime);
}
void UpdateDirectionToMove()
{
cameraForward = mainCamTransform.TransformDirection(Vector3.forward);
cameraForward.y = 0;
cameraForward = cameraForward.normalized;
cameraRight = mainCamTransform.TransformDirection(Vector3.right);
cameraRight.y = 0;
cameraRight = cameraRight.normalized;
DirectonToMove = Input.GetAxis("Horizontal") * cameraRight + Input.GetAxis("Vertical") * cameraForward;
Debug.Log(DirectonToMove);
}
I want the player to always be facing forward with respect to the camera's forward-facing direction, and currently this is happening correctly. The issue I am having is that I can't seem to move in a direction I am not facing. If I replace "transform.roation = Quaternion.LookRotation(cameraForward);" with the line "transform.rotation = Quaternion.LookRotation(DirectionToMove);" I can move in any direction I please - but I don't get the rotation I want. Any ideas?
I ran a quick test. Your code worked fine for me. Any chance you have a game object with a collider childed to the character controller object?
Thanks! That was exactly the problem. Although, I'm a little confused as to why colliders which are childed to a moving controller are "hitting" that controller...
Your answer
Follow this Question
Related Questions
Player model rotates forward with camera 0 Answers
[2D] CharacterController rotation 0 Answers
How can I get my model to face the direction it is moving? 2 Answers
How to change the angle my character falls at 1 Answer
Movement with physics 1 Answer