- Home /
How do I rotate using a negative angle value?,How to get the negative angle of an object?
I'm making a game where the player can rotate a platform. However, I don't want the player to be able to rotate the platform completely around. To prevent this I attempted to add if statements that would check the current value of the platforms angle, and if the angle was more or less than a specific angle it would continue to rotate when a specific key is being pressed. I understand that a negative angle isn't technically a thing but Unity's transform inspector shows a negative angle rather than passing 180 degrees. Is there a way to call that number rather than a number from 360? If not then might someone have an alternate suggestion on how to proceed with my goal?
Here's an example of the code I'm using.
void Update()
{
if (Input.GetKey(KeyCode.W))
{
if (gameObject.transform.rotation.eulerAngles.x < 20)
{
transform.Rotate(0.5f, 0, 0);
}
}
if (Input.GetKey(KeyCode.S))
{
if (gameObject.transform.rotation.eulerAngles.x > -20)
{
transform.Rotate(-0.5f, 0, 0);
}
}
}
Answer by Zaeran · Jan 28, 2020 at 08:33 AM
You could possibly add in a
float rotation = gameObject.transform.eulerAngles.x;
if(rotation < 0){
rotation += 360;
}
Then use the rotation variable to test your angles rather than the gameobject angle
Answer by Namey5 · Jan 28, 2020 at 08:40 AM
Here's a helper function I use to find the signed equivalent of Euler angles;
public static Vector3 GetSignedEulerAngles (Vector3 angles)
{
Vector3 signedAngles = Vector3.zero;
for (int i = 0; i < 3; i++)
{
signedAngles[i] = (angles[i] + 180f) % 360f - 180f;
}
return signedAngles;
}
Just pass in your transform's angles and store the result.