Character Rotation using CharacterController

Hello,
I need to have my character rotate using character controller.
I tried almost everything but nothing really works properly.
There is no help that I can find that makes my search easier.

This is part of my script.

void Update () {
		
	if(IsControlled){
		
		if(MoveDir.x>0){transform.localScale = MarioSize;}
			
		if(CC.isGrounded){
				MoveDir = new Vector3 (0,0,Input.GetAxis("Vertical")*Speed*ExtPwr);
				MoveDir.y = 0;

PLease answer with C# because that’s what I need.

And for those who wonder yes its a Mario game.
Thanks

Hi, I added this to my project a few days ago. Might not be the best way to solve it, but it works.

		if(Input.GetAxis("Horizontal") > 0)
			model.transform.localRotation =  Quaternion.Euler(new Vector3(0, 90, 0));
		else if(Input.GetAxis("Horizontal") < 0)
			model.transform.localRotation = Quaternion.Euler(new Vector3(0, 270, 0));

I’m using model.transform, because the script is attached to an object that is NOT the playermodel. If in your case it is the same object, you can just use transform.localRotation instead of model.transform.localRotation.

Where’s your rotation code? And what’s MoveDir?

Anyway, the CharacterController is a special capsule collider that is always aligned to the world Y axis. You can use transform.Rotate to rotate the character, but its capsule collider isn’t affected. If you want to rotate only in the horizontal plane only (about Y axis), you can use this:

public float rotSpeed = 90; // rotate speed in degrees/second

void Update () {
    if(IsControlled){
       transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
       if(MoveDir.x>0){transform.localScale = MarioSize;}
       ...

This rotates the character in the horizontal plane with the “Horizontal” axis.

I am using a characterController with my player currently, and this is how my movement code looks:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

	public int groundSpeed = 5;
	public int airSpeed = 3;
	public int jumpHeight = 1;

	private Vector3 moveDirection = Vector3.zero;
	private float gravity = 20.0f;

	
	void Update () {
		CharacterController controller = GetComponent<CharacterController>();
		

		if(controller.isGrounded) {
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
			moveDirection *= groundSpeed;
				
			if(Input.GetButton("Jump")) {
				moveDirection.y = jumpHeight;
				}
			}
			
			else if(!controller.isGrounded && Mathf.Abs(moveDirection.x) < airSpeed) {
				moveDirection.x = Input.GetAxis("Horizontal") * airSpeed;
			}

            //rotating here
			transform.right = Vector3.Slerp(transform.right, Vector3.right * Input.GetAxis("Horizontal"), 0.1f);
			
		}
		
		
		moveDirection.y -= gravity * Time.deltaTime;
			
		moveDirection.z = 0;
			
		controller.Move(moveDirection * Time.deltaTime);
	}

}

It is 2.5D, so 2D gameplay on 3D objects. I removed some of the project specific code that would only serve to confuse you, so I am not entirely sure it is bug free.