Код: Выделить всё
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;
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... left-limit
Мобильная версия