- Home /
Question by
Hawkeye91803 · Feb 22, 2021 at 05:59 PM ·
rotationmovementcharactercontroller
Why is my character rotating up/down, but not left/right?
I know I'm missing something obvious here, but I can't figure out what it is. Thanks everyone.
public class GliderScript : MonoBehaviour
{
private CharacterController controller;
public Transform glider;
private float baseSpeed = 100.0f;
public float mouseSensitivity = 100f;
public float xRotation = 0f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController>();
}
private void Update()
{
Vector3 moveVector = transform.forward;
if (!Input.GetMouseButton(1))
{
float mouseX = Input.GetAxisRaw("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
controller.Move(moveVector * Time.deltaTime * baseSpeed);
}
}
Comment
Best Answer
Answer by MUG806 · Feb 22, 2021 at 06:10 PM
Every frame you are setting the y rotation back to 0 here:
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
Maybe try something like:
transfrom.localEulerAngles = new Vector3(xRotation, transform.localEulerAngles.y, transform.localEulerAngles.z);