1
votes

I know there's a million questions on rotating, I've tried and read many things but I can't seem to solve this particular problem.

The only way I can think to explain it properly is with some poorly drawn diagrams so here goes!

I have a parent object, and a child object (here it's represented as a die where opposite faces add up to 7), the axes in this diagram are the parents X, Y, and Z directions

Starting point:

Starting point

I want the child object to have two methods that I'll call RotateZ and RotateX. RotateZ and RotateX should rotate the child dice around the parents Z and X axes in 90 degree steps.

RotateX:

Starting pointRotateX

RotateZ:

Starting pointRotateZ

The code I currently have is this

void RotateZ(int value) {
    transform.rotation *= Quaternion.AngleAxis(90f * value, pivot.transform.forward);
}

void RotateX(int value) {
    transform.rotation *= Quaternion.AngleAxis(90f * value, pivot.transform.right);
}

I should note pivot is the parent object

This appears to work great if the child starts at (0, 0, 0) rotation, repeatedly calling RotateZ or RotateX with (0, 0, 0) starting point works great. This problem comes if for example I call RotateX then RotateZ.

After the initial RotateX call it will look like this, giving the child a rotation of (90, 0, 0).

Starting point RotateX

Then after a RotateZ the desired result is this:

After RotateX enter image description here

But instead I end up with this:

After RotateX After RotateXZ

I'm really at a loss as to what I'm doing wrong. I have a feeling I've hit the limit of what I can do with Euler angles and I need to learn quaternions to prevent this problem. What rotation function do I need to get the desired result?

1
You have a spectacular problem. Never use Quaternions for any reason. It is very confusing they are mentioned in the Unity manual; they are only for specific internal use totally irrelevant to any normal game programming. In any event, all you are looking for is RotateAround. EnjoyFattie
Note too that you may well simply want to set the .eulerAngles.Fattie
@JoeBlow I was using euler angles originally and had the exact same issue. I know there's limitations with euler angles where they wont do the job which is why quaternions exist. I thought this might be one of those cases which is why I tried switching the implementation. I know they're more confusing and less easier to use but if run into gimbal lock you dont have a choice but to use them, I don't think your sentiment on never using them is correct.GP89
that's easy: I'll explain in an answer.Fattie
i'll also give you an absolutely critical tip.Fattie

1 Answers

2
votes

It looks like what you need is the really awesome call

RotateAround

it's one of the most important in Unity. RotateAround doco

Don't forget RotateAround rotates around a vector - ANY vector.

Huge tip...

A ubiquitous technique in video game engineering is: you use markers" - meaning just an empty game object - which you attach to objects or to which you attach your objects.

Very often, you temporarily attach an object to a marker, so as to move it, and then remove it from the marker again.

Here's how to do it in code. Make a marker "controlRotator"...

  1. make controlRotator a child of your controlObject.

  2. make it perfectly straight locally: ie, simply exactly the same orientation as the parent, controlObject, ie, local rotation = identity

  3. next, cleverly set the WORLD position of controlObject to exactly the WORLD position of smallCube...

  4. your smallCube, make a note of it's parent. take smallCube off that parent.

  5. next, temporarily make smallCube a child of controlRotator

... and now you can ...

  1. rotate the controlRotator any way you like!

You simply rotate controlRotator, not the small cube.

Note that you are now doing exactly what you asked to do, you're rotating smallCube using the axis of the controlObject. Clever right?

  1. finally, put smallCube back on to the parent you saved in step 4.

This is exactly how you do it. In practice this is so commonplace that you'll have an extension or function to do it.

Note that it is fine to make the GameObject controlRotator on the fly as needed, and get rid of it after the twist. (It's only a mathematical abstraction.)

Further tip...

This operation should work "on" controlObject. Make a script called

TwistSomethingOnMyAxis.cs

and put that ON the CONTROLOBJECT.

It would have one call

 public void twistThisThing(Transform twistIt) {}

What you do is, when you want to twist SMALL CUBE, you actually call to the TwistSomethingOnMyAxis ON THE CONTROL OBJECT. You see? So like

TwistSomethingOnMyAxis:twistor = 
  controlObject.GetComponent<TwistSomethingOnMyAxis>();
twistor.twistThisThing( smallCube , .. 90 degrees on Y or whatever .. );

I hope that makes sense! Enjoy!