Blueprints Get Rotation of Object

I am having a problem getting the rotation of an object in blueprints.Please view the imgur links for a full explanation of the problem. Anyone have any ideas?

Unreal will convert a value of -180 to 180? what? that definitely isnt the case why would the engine just change the value like that?. it wont change the value from a positive to negative so dont worry about that. however if you want to change the value from positive to negative you can use the absolute function.

Please could you take a look at the second Imgur link for a better explanation of the problem.

Try using AddActorWorldRotation instead of AddActorLocalRotation.

Problem

Unreal stores its rotators as a collection of float values, that each go from -90 degrees to +90 degrees depending on their direction.Take a look at this unit circle for reference: (Degree values will be multiplied by 90)

https://www.mathsisfun.com/geometry/images/unit-circle.svg

The reason why you are getting 360 degrees, down to 270 degrees and back up to 360 degrees again instead of continuing to count down to 180, is likely because you are calculating the angle of rotation relative to itself. So when your object turns, some of these values suddenly flip sign (+ to -, or vice versa) and the angle of rotation starts counting up instead of down, or jumps 180 degrees in either direction.

If I understand your animation correctly, you aren’t interested in your object’s rotation alone, you are actually looking for your object’s rotation compared to some static frame of reference. If we imagine a dimmer switch being attached to a wall, we are interested in how many degrees it has rotated from its initial position.


Code

This code calculates rotation between two vectors restricted to a plane, and outputs float 0 → 360 degrees of rotation.

InVector: Direction Vector from the object that rotates. (Example: DimmerSwitch UpVector)

VectorToMeasureAgainst: World direction vector to measure rotation against: (Example: World UpVector)

RotationalNormalVector: Normal of the plane where the rotation is confined to. (Example: Looking at the dimmer switch image above, we are interested in the vector that goes directly from the dimmer switch centre and straight into the camera.)

  1. The code first calculates the angle between the two vectors using dot product and scalar product and converts it into degrees.
  2. Then it checks the direction of the cross product of the two input direction vectors compared to the normal vector. If this has a positive sign, we are on the first half of rotation (0 - 180 degrees). If this has a negative sign, we are on the second half of rotation (180 - 360 degrees).
  3. Select the correct value and output.

Math reference: Angle between two vectors

1 Like

Thank you very much for this clear and thorough explanation, I really feel like you went above and beyond with this answer!

Happy to help! I was stuck for a long time on those same problems you encountered, and spent even longer trying to find a good solution.

Just as a final note: This solution assumes the two input vectors always are in the same plane, defined by the normal vector.