Мне нужна помощь в довольно распространённом вопросе - как сделать ограничение поворота объекта. На всех форумах я видел что это надо делать через Mathf.Clamp, но у меня это не сработало. Мой объект просто когда доходил до нуля возвращался в минимальную позицию, а когда я его вёл вниз там ограничения вообще не было.
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
public GameObject turrel;
public GameObject gun;
public float gunMin, gunMax;
public float speedRot;
void Update()
{
turrel.transform.Rotate(0f, 0f, Input.GetAxis("Mouse X") * speedRot, Space.Self);
gun.transform.Rotate(Input.GetAxis("Mouse Y") * speedRot, 0f, 0f, Space.Self);
gun.transform.localEulerAngles = new Vector3(Mathf.Clamp(gun.transform.localEulerAngles.x, gunMin, gunMax), gun.transform.localEulerAngles.y, gun.transform.localEulerAngles.z);
}
}
using System.Collections.Generic;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
public GameObject turrel;
public GameObject gun;
public float gunMin, gunMax;
public float speedRot;
void Update()
{
turrel.transform.Rotate(0f, 0f, Input.GetAxis("Mouse X") * speedRot, Space.Self);
gun.transform.Rotate(Input.GetAxis("Mouse Y") * speedRot, 0f, 0f, Space.Self);
gun.transform.localEulerAngles = new Vector3(Mathf.Clamp(gun.transform.localEulerAngles.x, gunMin, gunMax), gun.transform.localEulerAngles.y, gun.transform.localEulerAngles.z);
}
}