Limiting the Rotation around Z-Axis.

I’m developing a Flight Simulator app. I am newbie in this job. I want to limit rotation around z-axis between 80 and -80 degrees. My script are below. How can I do that? Please help me. Thanks :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class kontrol : MonoBehaviour
{
    protected DynamicJoystick joystick;
    public float speed = 100f;
    float maxspeed = 300f;
    float minspeed = 35f;
    float rotspeedvertical = 40f;
    float rotspeedhorizontal = 60f;
    private bool rotating = false;
     
    void Start()
    {
        joystick = FindObjectOfType<DynamicJoystick>();

    }

    void FixedUpdate()
    {
        Vector3 camPos = transform.position - transform.forward * 8f + Vector3.up * 4f;
        float bias = 0.8f;
        Camera.main.transform.position = Camera.main.transform.position*bias + (1.0f-bias)*camPos;
        Camera.main.transform.LookAt(transform.position+transform.forward*20f);

        transform.position += transform.forward * Time.deltaTime * speed;
        speed -= transform.forward.y*Time.deltaTime*40f;
        if (speed <minspeed)
        {
            speed = minspeed;
        }
       
        if (speed > maxspeed)
        {
            speed = maxspeed;
        }                         
       
       
       [B] transform.Rotate(new Vector3((-joystick.Vertical*rotspeedvertical*Time.deltaTime), 0f,((-joystick.Horizontal*rotspeedhorizontal*Time.deltaTime)))); [/B] 
       
    }
}

What you will need to do is to create a persistent Vector3 representing the Euler angles of your rotation, and add the joystick input values to that. You will be able to easily cap the rotation values to anything there, which you can’t do after it’s been converted to a Quaternion. Every frame, use transform.rotation = yourEulerVariable; rather than transform.Rotate() to assign the rotation.