How to get a change in transform rotation?

Hi, I’m trying to get the change (variation) on the transform.up rotation (turns left-right) of my player (first person shooter) gameobject from one frame to other to apply an effect.
The player gameobject turns are not controlled with mouse or keys because it is an VR project and it is using the camera (head) rotation.
I have tried with Vector3.Angle function but without success.
Any help would be welcome.
Thanks in advance
Angel

Quarternion are ideal for handling differences in rotation. It’s analogous to vectors for handling differences in position.
Maybe Quaternion.FromToRotation would be enough to do the job for you.

But that’s use only one axis. More generally, the delta rotation between two objects A and B is:

Quaternion deltaRotation = B.transform.rotation*Quaternion.Inverse( A.transform.rotation);

So, if you rotate the object A by deltaRotation, it will be oriented exactly as object B:

A.transform.rotation = deltaRotation*A.transform.rotation.
2 Likes

Thanks, but I don’t see how can it be used to check the variation in the transform.up rotation of one gameobject between two frames.

Let’s say you have the up vector from the previous frame (previousUp) and the up vector on the current frame (transform.up). Then the change in rotation is:

Quaternion deltaRotation = Quaternion.FromToRotation(previousUp, transform.up);

Then you can applied this rotation to another object to make it rotate by the same amount.

targetObject.transform.rotation = deltaRotation* targetObject.transform.rotation.

Beware that one vector is not enough to completely describe a rotation, because there are several rotations that are possible to make the vectors aligned. So, the second method might be better for your case.

2 Likes

Ok, but how can I know the amount of change in the rotation to apply an effect or not ?
Or at least if any change has happened. That function is using quaternions and I 'm afraid I don’t know how they work. :frowning:

You could get the angle between the previous up and and current up vector using Vector3.Angle.

I already tried that.
Thanks.

and did it solve your problem?

Edit:
@angel_m
Sorry. You’ve mentioned using Vector3.Angle in your OP. You could use it to detect whether the up vector has rotating by checking whether the angle is not zero. Then you can apply the rotation. But I think that’s overdoing, you don’t need to check whether there is a rotation: if the vector does not rotate the deltatRotation will compute to a non-rotation.

1 Like

I have to say I don’t know how quaternions work. How can I obtain the deltaRotation value and check if it is zero? Is not a Quaternion?

The quaternion is the deltaRotation. The quaternion component values are x,y,z,w. But those raw numbers are meaningless so you should not use them directly. All you need to know is that they encode (somehow) an angle and an axis, which you can retrieve with Quaternion.ToAngleAxis. That’s it: a quaternion is just an angle and an axis. Then you can check if the angle is zero.

1 Like

Quaternion.Angle will give you the difference in rotations.

1 Like

@angel_m
Using @GroZZleR 's suggestion, you would neet to store the transform.quaternion of the previous frame. You won’t need tranform.up anymore.

could you not store a StartRotation and a NewRotation and just compare both to each other?

I need to get the angel difference in a certain amount to apply an effect to camera.
Like this in pseudocode:

If (angleDifferenceY>30.0)
Fade Camera

Thanks to everybody.