Answer the question
In order to leave comments, you need to log in
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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question