Rotate a character controller in the direction of input axis.

Im using the character controller.move example

And im trying to rotate the player to the direction the character is moving with:

transform.LookAt(transform.position + movementVector);

However, this just makes the player rotate whenever any input is given on the horizontal axis, this causes the player to just keep turning whenever it moves sideways.

Preset directions wont work for me because im using joystick input, So diagonal directions have to work too.

I’ve tryied something out, see if this is what you’re looking for: (It ignores Y axis as movement)

private CharacterController CharCtrl;

    private void Start()
    {
        CharCtrl = this.GetComponent<CharacterController>();
    }

    private void Update()
{
    Vector3 NextDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));

    if (NextDir != Vector3.zero)
        transform.rotation = Quaternion.LookRotation(NextDir);

    CharCtrl.Move(NextDir / 8);
}

Know this is an old thread but I was having a similar issue. While the character would rotate properly, it was only moving to the right. Note that I am using the reference script from the CharacterColtroller.Move API

The fix ended up being to multiply the move speed by -1 (if it is a positive number) if you were facing left and want to move left. You also need to make sure that it is returned to a positive number once you want to rotate and move to the right.

I find A Solution And Donot Ignore y Axis And Gontain Gravity, Working when Spawning
thats Work:

public Joystick joystek;

public float movement_Speed;

CharacterController charCtrl;

public static float xMove, zMove;

//private Vector3 facDir;

void Awake()
{
    charCtrl = GetComponent<CharacterController>();
}

void Update()
{
    playerMovmentControler();
    playerRotationControler();
}

// Update is called once per frame
void playerMovmentControler()
{
    xMove = Input.GetAxis("Horizontal");// + joystek.Horizontal;
    zMove = Input.GetAxis("Vertical");// + joystek.Vertical;

    float gravity = 9.8f;
    Vector3 moveAxis = new Vector3(xMove, -gravity, zMove);
    charCtrl.Move(((moveAxis) * movement_Speed * Time.deltaTime));
}

void playerRotationControler()
{
    if (xMove > 0)
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 2f * Time.deltaTime);
    else if (xMove < 0)
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 2f * Time.deltaTime);

    if (zMove > 0)
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 2f * Time.deltaTime);
    else if (zMove < 0)
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 2f * Time.deltaTime);
}

When you Spawning Charactercontroller upper to the Surface Use this to proper rotation with Gravity