Check if Quaternion hasn't been modified

Hello!

I’ve run into a bummer (I’m probably not understanding something here). Basically, I’m making a simply AI script, and I’m adding an offset to a target rotation like this:

TargetRotation = Quaternion.LookRotation(Heading);
        TargetRotation *= AvoidanceOffset;

However, if I haven’t set AvoidanceOffset to anything, Quaternion.LookRotation seems to have no effect and the object doesn’t rotate. So I decided to do this:

if(AvoidanceOffset != Quaternion.identity)
            TargetRotation *= AvoidanceOffset;

That way if I hadn’t set AvoidanceOffset to anything, it wouldn’t try to add it. However, the if statement is never triggered.

So I took it an extra step:

if(AvoidanceOffset != Quaternion.Euler(0, 0, 0))
            TargetRotation *= AvoidanceOffset;

Yet the inspector shows AvoidanceOffset to be 0, 0, 0, 0.

So my question is, how can I check if a Quaternion is = to 0, 0, 0, 0?

Thank you!!

1 Like

Check this out :slight_smile: XNA Game Studio 4.0 Refresh | Microsoft Learn

What’s it matter.

If AvoidanceOffset wasn’t set, the multiplication of it will result in no change… if it was, it will change. Isn’t that expected behaviour?

Not sure if you’re talking to me or the OP, but Quaternion.identity = { 0, 0, 0, 1 } is why I posted my link.

1 Like

Talking to OP.
(I’m operating under the idea that AvoidanceOffset is a serialized field, which would mean ‘not-set’ would make it the identify quaternion from the get go, since that’s the default quat value when unity deserializes… but yes, if it’s not serialized, then they should be setting it to identity from the get go)

Yep, honestly I didn’t really know the answer and thought as much as you. I didn’t even know identity meant 0,0,0,1 until I looked it up - but posted because he said to compare to 0,0,0,0 :slight_smile:

Also, at OP… if you’ve got an actual zero quat, and that’s what you want to rule out.

Either a default(Quaternion) or constructing a zero Quaternion will work (new Quaternion(0f,0f,0f,0f)):

Though honestly, having a zero quat means either you setup your scenario incorrectly, or you did some maths that screwed things up really bad.

A quick Debug.Log tells me Quaternion.Identity is 0,0,0,1, so that’s my bad.

Thanks for the help! Turns out that my assumption that Quaternion.Identity was 0,0,0,0 was the problem. I simply did this:

if (AvoidanceOffset != Quaternion.identity)
            TargetRotation *= AvoidanceOffset;
        else
            Debug.Log("EQUAL!");

And set AvoidanceOffset in the Inspector to 0,0,0,1:
3121564--236248--upload_2017-6-25_16-16-38.png

And it works great :smile:

But why even bother?

Multiplying by the identity quaternion is like multiplying 5 by 1… you still get 5. It’s why it’s called the ‘identity’ vector.

You should be able to just get away with:
TargetRotation *= AvoidanceOffset

And why is your inspector showing the quat as its imaginary parts… why is it not showing as euler? Do you expect people to set the actual quaternion values? I’m good with quats, and I don’t even want to have to sit down and do the maths to calculate the appropriate values for x,y,z,w.

1 Like

You’re completely right. Because I set AvoidanceOffset to 0,0,0,0, it wasn’t rotating. So I thought that 0,0,0,0 was Quaternion.Identity and that somehow was causing it to not rotate. As soon as I set AvoidanceOffset, the 4th number becomes non-zero and it would rotate properly.

Therefore, I didn’t even need this if I had simply set it correctly the first time. I took out the if statement and it behaves the same.

Can I make the Inspector show Euler angles? I’d love that; it always shows me non-Euler angles for Quaternions.

Thank you for pointing that out, I learned something new today!

Sorry, my bad. I’m so used to my framework that I forgot that Unity doesn’t use a euler inspector by default for Quaternions.

Bad Unity, bad!

But yeah, I use a custom PropertyDrawer for my Quaternions always. Cause srsly, the values of a quat are unintelligible to the average user.

Here’s the actual field code:
https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBaseEditor/SPEditorGUI.cs#L747

And the PropertyDrawer I use:
https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBaseEditor/Base/Inspectors/EulerRotationInspectorPropertyDrawer.cs

There’s other’s with less dependencies out there… like here’s a really simple one:
http://answers.unity3d.com/questions/1314080/use-euler-angles-for-quaternion-variable-in-the-in.html

Whenever I’ve needed to know the angles of something I just spam the Console. Awful but simple.

Some things just baffle me with Unity. They show Euler angles in the Transform component, but they can’t show them in your scripts? That’s either extreme laziness or bad coding.

I’ll definitely check out the Spacepuppy framework since Quaternion Euler angles in the Inspector would be a great feature to have. Maybe Unity will buy it like they did with TextMeshPro :wink:

Oh, don’t take this entire framework just to get euler angles in the editor, like I said, there’s simpler ones out there which I linked.

I actually have to do some pruning to this framework… over the years it has gained some things that turned out we NEVER use. Just haven’t had the time to do the refactoring since we’re in the midst of developing.

1 Like