- Home /
Obtain angle from given rotation
Hi there!
I've been searching around the Community and I didn't find a suitable response for me, or at least I cannot get the possible solutions working on my project.
I want to simulate wind on a 2D environment, making this wind affecting an object. I get a random angle between 1 and 360 and compare it with the angle of the object. I'm obtaining this angle with:
Quaternion.Angle(transform.rotation, Quaternion.identity)
The problem is that this value varies between 0 and 180, unsigned, no matter the rotation of the object. I need to know the sign because if the angles are the same, the object reaches maximum speed in the direction it is facing, stopping if they are contrary.
How can I obtain the object's angle having its rotation?
Thanks!
Answer by slavo · Dec 15, 2017 at 12:10 PM
Hi if you are in 2d, only angle you need to take care is you Euler Z. It it angle relative to Right Vector
float angle = transform.rotation.eulerAngles.z; //0 - 360 comapring to Vector2.right anti clockwise
float signedAngle = angle > 180 ? angle - 360f : angle; //-180, 180 comapring to Vector2.right
Your answer
Follow this Question
Related Questions
How Do I accurately rotate a player 180 degrees about the Z axis? 1 Answer
Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer
3D nested Turret Prefab Rotation 1 Answer
Random perpendicular vector3 after rotation 1 Answer
Problem with rotation around axis 1 Answer