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.
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.
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:
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.