How do I lock my Players Y axis to my Cameras?
I've been messing around with trying to build some form of an FPS game the past 3 days, and I've gotten myself into using mouse input for my camera. I've therefore encountered this rather annoying problem where I can move my camera around, but my player object does not follow its Y-axis which is what I want.
How would I be able to add this to my code?
My Camera Script looks as following ( Please note I'm entirely new to Unity and I've used help online for the code ):
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Mouse : MonoBehaviour { public float sensitivityX = 15F; public float sensitivityY = 15F;
public float minimumLockX = -60F;
public float maximumLockX = 60F;
public float minimumLockY = -360;
public float maximumLockY = 360;
public Camera CameraObject;
public GameObject PlayerObject;
private float rotationX = 0;
private float rotationY = 0;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
rotationY += Input.GetAxis("Mouse X") * sensitivityY;
rotationX += Input.GetAxis("Mouse Y") * sensitivityX;
rotationX = Mathf.Clamp(rotationX, minimumLockX, maximumLockX);
CameraObject.transform.localEulerAngles = new Vector3(-rotationX, rotationY, 0);
if (Input.GetKey(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
}
Answer by frumsy · Sep 30, 2020 at 11:37 AM
Managed to solve this myself.
By setting the rotation of the Y-axis to my player and not my camera whilst still having the rotation of the X-axis set to my camera.
PlayerObj.transform.localEulerAngles = new Vector3(0, rotationY * 3, 0);
CameraObject.transform.localEulerAngles = new Vector3(-rotationX, 0, 0);
Your answer
![](https://koobas.hobune.stream/wayback/20220613012825im_/https://answers.unity.com/themes/thub/images/avi.jpg)