How do I make a CharacterController look where it's moving?

Basically the title… I have a character controller that moves using the CharacterController.Move Function, and I need it to always be looking where it’s moving.

This should do the trick: (untested)

controller.Move(moveDirection * Time.deltaTime);
moveDirection.y = 0;
transform.rotation = Quaternion.LookRotation(moveDirection);

I’m not entirely sure how to put this in… I actually have two move functions controlling the direction…

//camPointer is a gameObject that is always looking at my camera so movement is relative to the camera
controller.Move(camPointer.forward * -Input.GetAxis("Vertical") * verticalSpeed * Time.deltaTime);
controller.Move(camPointer.right * -Input.GetAxis("Horizontal") * horizontalSpeed * Time.deltaTime);

In this case, it looks like you find the movement direction in two separate steps. First, you find the forward component of the movement direction and then you find the sideways component. You could just combine the two directions into a single movement vector.

C#

Vector3 movementDirection = (camPointer.forward * -Input.GetAxis("Vertical") * verticalSpeed) + 
    (camPointer.right * -Input.GetAxis("Horizontal") * horizontalSpeed);
controller.Move(movementDirection * Time.deltaTime);
movementDirection.y = 0;
transform.rotation = Quaternion.LookRotation(movementDirection);

Ok thank’s… I’ll try it out

Ok It’s working now =D

I had to change the last line a bit though… I ended up with

            Vector3 movementDirection = (camPointer.forward * -Input.GetAxis("Vertical") * verticalSpeed) + 
                (camPointer.right * -Input.GetAxis("Horizontal") * horizontalSpeed);
            controller.Move(movementDirection * Time.deltaTime);
            movementDirection.y = 0;
            transform.rotation = Quaternion.LookRotation(new Vector3(-movementDirection.z, 0, movementDirection.x));