Limitation of rotation not working

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

public class Rotacion : MonoBehaviour {


    public int x;
    public int y;
    public int z;
    public float zs = 0f;

    // Use this for initialization
    void Start () {
        zs = Mathf.Clamp(zs, -15, 15);

    }

    // Update is called once per frame
    void Update () {
        transform.Rotate(new Vector3(x, y, zs) * Time.deltaTime);

    }
}

idk why the Z is not limited :frowning:

Rotate rotates by an a vector 3, so you’re rotating the ZS (meaning you rotate by ZS and ZS is 15, if you were to rotate twice you’d rotate 30 degrees) You want to set the transform.rotation using Euler Angles I believe.

I’d do something like this:

// Update is called once per frame
    void Update ()
    {
        if (transform.rotation.z <= 15 && transform.rotation.z >= -15)
            transform.Rotate(new Vector3(1, 2, 3) * Time.deltaTime);
        if (transform.rotation.z < -15)
            transform.Rotate(new Vector3(transform.rotation.x, transform.rotation.y, -15));
        if (transform.rotation.z > 15)
            transform.Rotate(new Vector3(transform.rotation.x, transform.rotation.y, 15));
    }

Might be hairy, but it would work.

Also, you can look into this post to see how Quaternion.Angle is used for clamping rotations: Clamping LookRotation angle - Questions & Answers - Unity Discussions