Given that you're lockingIt sounds like you want to project the mouse cursor:
Cursor.lockState = CursorLockMode.Locked;
...it looks like you're trying onto the horizontal plane, and rotate to implement FPS-style controlsface that. You couldWe can do that like soeasily:
public class Move : MonoBehaviour
{
public float pitchSpeed = 1f;
public float yawSpeed = 1f;
public float maxPitch = 90f;
// Update Vector2is _angles;called once per
frame
void StartUpdate()
{
// Record our initial orientation.
Get the position of the mouse on the _anglesscreen, =in transformpixels.localEulerAngles;
Cursor.lockStateVector2 =screenPosition = CursorLockModeInput.Locked;mousePosition;
// UsuallyCast we'dthis hidepoint through the cursorcamera whileviewpoint/projection
locked, but I'll leave this as-is.
// to form a ray shooting Cursor.visiblethrough =world true;space.
}
//Ray UpdateworldRay is= calledCamera.main.ScreenPointToRay(screenPosition);
once per frame
void Update()
// Form a horizontal plane through {this object's origin.
//Plane AccumulateworldPlane our= yawnew Plane(left-right rotation)Vector3.up, and wrap aroundtransform.position);
_angles.y// =Check (_angles.yif +the yawSpeedray *from Input.GetAxis("Mousethe X"))cursor %hits 360this plane.0f;
// Accumulate our pitch (up-downIf rotation)not, and clamp at theskip polesit.
_angles.x =if Mathf(worldPlane.ClampRaycast(
worldRay, out float distance) {
_angles.x// -If pitchSpeedwe *got Input.GetAxis("Mousea Y")hit,
get the world space coordinates
-maxPitch,
// of the point on the plane where the cursor ray hits.
maxPitch
var worldPosition = worldRay.GetPoint(distance);
// Apply these rotation angles// Rotate to thislook transformtoward that point.
transform.localEulerAnglesLookAt(worldPosition);
= _angles; }
}
}