Question by
goodborb · Dec 15, 2019 at 11:06 AM ·
rotationfirst-person
Trying to get a basic first person camera working only one axis rotating
my code is parented to a camera in unity 3d, for some reason, it is only letting me move on one axis at a time. When I comment out a Quatornian.AngleAxis line suddenly the other line will start working and I can smoothly rotate the camera with the mouse on that axis.
public class CameraMovement : MonoBehaviour
{
[SerializeField] private float multiplier = 100.0f;
private Transform cameraTransformControls;
[SerializeField]
private float xRotation, yRotation = 0;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
cameraTransformControls = this.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
//Camera.main.ScreenToViewportPoint(Input.mousePosition)
//cameraTransformControls.Rotate(Input.GetAxis("Mouse Y") * multiplier, -Input.GetAxis("Mouse X") * multiplier, 0.0f, Space.World);
float mouseX = Input.GetAxis("Mouse X") * multiplier * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * multiplier * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
yRotation -= mouseX;
transform.localRotation = Quaternion.AngleAxis(xRotation, Vector3.right);
transform.localRotation = Quaternion.AngleAxis(yRotation, Vector3.up);
}
Comment
Answer by Wilomomo · Dec 15, 2019 at 02:49 PM
Let me correct your script :
float mouseX = Input.GetAxis("Mouse X") * multiplier * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * multiplier * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
yRotation -= mouseX;
transform.localEulerAngles = xRotation * Vector3.right + yRotation * Vector3.up;