If I correctly understood the wording of the problem, I wrote from below, sort of like, the working script code for an object that cannot be rotated more than 30 degrees to the left and 30 degrees to the right. Because in Unity Transform.rotation varies from 0 to 1, we use transform.localRotation.eulerAngles . And one more thing: it turns out that when our object is in its original position (i.e., the angle of rotation along Z = 0), that is, there are 2 options for deviation - either 30 to the left or 330 to the right. Those. not -30. And because of this, it turned out more conditions than it could be at -30.
private const float Speed = 0.1f; private const float FlySpeed = 0.1f; private const float RotateSpeed = 5f; private const float MaxRotation = 30f; private const float MinRotaion = 360f - MaxRotation; Rigidbody rigidBody; private void Start() { rigidBody = this.GetComponent<Rigidbody>(); } void Update() { //получаем угол поворота по Z float rotationZ = this.GetComponent<Rigidbody>().transform.localRotation.eulerAngles.z; float x = CrossPlatformInputManager.GetAxis("Horizontal"); float y = CrossPlatformInputManager.GetAxis("Vertical"); transform.Translate(Vector3.forward * Speed * Time.deltaTime); rigidBody.AddForce(transform.up * FlySpeed * Time.deltaTime); if ((x > 0 && rotationZ + (RotateSpeed * x) < MaxRotation) || rotationZ + (RotateSpeed * x) > MinRotaion || (x < 0 && (rotationZ + (RotateSpeed * x) < MinRotaion) && (rotationZ + (RotateSpeed * x) > MinRotaion) || (rotationZ + (RotateSpeed * x) < 30))) { transform.Rotate(new Vector3(0, 0, RotateSpeed * x)); } }