N
N
Naked_Salmon_studio2021-05-08 14:43:25
C++ / C#
Naked_Salmon_studio, 2021-05-08 14:43:25

How to limit rotation range in unity?

I'm probably the dumbest programmer, but I don't understand how to make a range of rotation. I have an arrow on the speedometer that should spin from -20 to 200 degrees, but for me it spins like crazy without stopping.

public GameObject target;
    public GameObject arrow;
    private float Rotate = 20;
    public Transform pixel_art_1;

    private void Start()
    {
        Transform transformm;
       transformm = GetComponent<Transform>();
    }
        void Update()
    {
        transform.RotateAround(target.transform.position, Vector3.back, Rotate * Time.deltaTime);
        if (transform.rotation.z >= 240)
        {
                transform.RotateAround(target.transform.position, Vector3.back, Rotate * -1 * Time.deltaTime);  
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Prizm, 2021-05-08
@PrizmMARgh

void setAngle(float angle) {
angle = angle > LIMITMAX ? LIMITMAX : angle < LIMITMIN ? LIMITMIN : angle; // limit the angle within [LIMITMIN; LIMITMAX]
transform.rotation.z = angle; //(or something like setRotation, but definitely not rotate, I don't remember exactly how the methods are called)
}
if you want the arrow to rotate more smoothly, you can write:
float beta = 0.9; //the closer it is to 1, the slower it rotates
transform.rotation.z = (transform.rotation.z) * (1-beta) + angle * beta; //again, it's probably not possible by standard to set the rotation value directly, so a set method would be required.

I
IDzone-x, 2021-05-24
@IDzone-x

Well, I think you're here Mathf. Clamp (); come in handy. Go to the dock for details

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question