So im trying to make Sea Of Thieves like-game, but i got this problem and i dont know how to fix it. im sitting at least 1 hour at it and i still dont know. im trying to set up a camera limit in y axis when using a steerwheel, but theres this problem that when i reach left limit, the camera is teleporting to the right limit. im using 90f + limit because otherwise player is rotated to the left. Im gonna send the code in next comment because i have character limit.
#Unity Problem
1 messages · Page 1 of 1 (latest)
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
float yRotation = 0f;
private bool isLerping = false;
private bool isSteeringWheel;
[SerializeField] public int YCamLimitWhenSteering = 70;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if (isLerping) return; // Zablokuj kontrolę nad kamerą, jeśli jest lerpowanie
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// Rotacja osi X (pionowa)
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
if (isSteeringWheel)
{
yRotation += mouseX;
// Oblicz nowe ograniczenia
float minY = 90f - YCamLimitWhenSteering;
float maxY = 90f + YCamLimitWhenSteering;
if (yRotation < minY)
{
yRotation = minY;
}
else if (yRotation > maxY)
{
yRotation = maxY;
}
playerBody.localRotation = Quaternion.Euler(0f, yRotation, 0f);
}
else
{
playerBody.Rotate(Vector3.up * mouseX);
}
}
public void SetSteering(bool isAtWheel)
{
isSteeringWheel = isAtWheel;
// Jeśli gracz zaczyna sterować, ustaw yRotation na aktualną rotację
if (isAtWheel)
{
yRotation = playerBody.localRotation.eulerAngles.y;
}
}
public void SetLerping(bool lerping)
{
isLerping = lerping;
}
}
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
float yRotation = 0f;
private bool isLerping = false;
private bool isSteeringWheel;
[SerializeField] public int YCamLimitWhenSteering = 70;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if (isLerping) return;
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);
if (isSteeringWheel)
{
yRotation += mouseX;
float minY = -YCamLimitWhenSteering;
float maxY = YCamLimitWhenSteering;
yRotation = Mathf.Clamp(yRotation, minY, maxY);
playerBody.localRotation = Quaternion.Euler(0f, yRotation, 0f);
}
else
{
playerBody.Rotate(Vector3.up * mouseX);
}
}
public void SetSteering(bool isAtWheel)
{
isSteeringWheel = isAtWheel;
if (isAtWheel)
{
yRotation = playerBody.localRotation.eulerAngles.y;
if (yRotation > 180)
{
yRotation -= 360;
}
}
}
public void SetLerping(bool lerping)
{
isLerping = lerping;
}
}
how about this?
Clamp Y Rotation: Instead of using 90f + YCamLimitWhenSteering, I've centered the rotation clamping around 0 (e.g., between -YCamLimitWhenSteering and YCamLimitWhenSteering). This keeps the camera from "snapping" when rotating beyond limits.
Normalize Y Rotation: I've added normalization for yRotation to bring angles greater than 180 degrees back into the -180 to 180 range. This prevents weird behavior when rotations exceed 360 degrees.
Simplified Limit Logic: The new limits directly control the left and right steering bounds, which makes the rotation system cleaner.
🤔
Polish jumpscare

im sorry ive abandoned this project but thank you for answer maybe ill come back to it later