- Home /
Question by
SilkiesRBest · Dec 19, 2021 at 11:29 PM ·
c#lookrotation
why has it stopped me being able to look left and right?
hi im a begginer and im following a tutorial made by brackeys and im confused on why it has stopped me being able to look left and right.
heres the code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class mouselook : MonoBehaviour { public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("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);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Comment
Answer by BurakKaya · Dec 28, 2021 at 05:09 AM
You clamped the wrong axis.
X is usually horizontal Y is vertical
So, probably this will fix your problem
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
yRotation -= mouseY;
yRotation = Mathf.Clamp(yRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(0f, yRotation, 0f);
playerBody.Rotate(Vector3.up * mouseY);
}
Your answer