Limit rotation of a plane?
I have a plane that a ball rolls around on that I'm rotating using input accelerometer values from a tablet. How can I limit the rotation in the x and z axis?
This is my current code:
private Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
float getHorizontal = Input.acceleration.x;
float getVetrical = Input.acceleration.y;
transform.Rotate (getVetrical, 0, -getHorizontal);
}
Answer by amiel_ace · Apr 22, 2016 at 12:53 PM
Use a simple if statement:
private Rigidbody rb;
private float maxRotX = 45, maxRotZ = 45;
float getHorizontal = 0, getVetrical = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (transform.eulerAngles.x < maxRotX && transform.eulerAngles.x > -maxRotX)
getHorizontal = Input.acceleration.x;
if (transform.eulerAngles.z < maxRotZ && transform.eulerAngles.z > -maxRotZ)
getVetrical = Input.acceleration.y;
transform.Rotate(getVetrical, 0, -getHorizontal);
}
For some reason the plane now rotates 180 in the x axis and stops, (I have changed the max values to 10 degrees as this is the limit I want)
Ok I stopped it from doing that, but this hasn't limited the angles, its exactly the same as before.
Answer by toromano · Apr 22, 2016 at 01:49 PM
If you are not going to exceed 90 degree and deal with singularity issues, following modifications might work for you:
void Update()
{
if (transform.eulerAngles.x < maxRotX && transform.eulerAngles.x > -maxRotX)
getHorizontal = Input.acceleration.x;
else
getHorizontal = 0f;
if (transform.eulerAngles.z < maxRotZ && transform.eulerAngles.z > -maxRotZ)
getVetrical = Input.acceleration.y;
else
getVetrical = 0f;
transform.Rotate(getVetrical, 0, -getHorizontal);
}
It would seem that ins$$anonymous$$d of going negative the angle goes from 0 to 359 ins$$anonymous$$d of -1, so this wouldn't work