Lookat offset angle combined with RotateAround. Basically orbit + rot angle

Hello world!

I am having a bit of trouble with making a object orbit around another object on specific angle.

Basically what i have is this:

transform.RotateAround(orbitAround.transform.position, Vector3.forward, 5 * Time.deltaTime);	
transform.LookAt(orbitAround.transform.position, Vector3.back);

Which produces a working-ish result of this:

alt text

But what i was aiming for was more like this:

alt text

I am guessing there is a simple way to “lookat” an object with an offset angle of the looking object, but i cannot find how to do it.

Any help will be appreciated :slight_smile:

This happens because your ship’s nose is aiming at the Z axis originally, which makes it look at the planet. What you can do is 2 options.

1: Make empty group and parent the ship in it. Reset ship’s transformations and rotate the it so the X Axis of the group is where the ship’s nose is aiming. Attach that script to the group, instead to the ship.

2: More simplified version i think is, just make one group, Position it at the center of the planet and then parent the ship inside. This way you will be able to rotate the ship as you want and just make the group to rotate around it’s pivot on the desired axis :slight_smile:

The other answers might work better, but to directly answer your question, rotations are Quaternions, and are applied using the multiply symbol. A 90 degree-left spin is Quaternion.Euler(0,-90,0);.

So: transform.rotation = transform.rotation * Quaternion.Euler(0,-90,0); spins 90 left.

Tricky parts are: “times Q” will apply it as a local rotation, so will always spin to your current left. If the shuttle isn’t facing “up” on it’s orbit, that spin won’t work Setting the 2nd LookAt parm to the rotation axis would make the shuttle spin “rotation up” while it looks at the earth. Or using “Q times” will use Q as a global rotation. Quaternion.AngleAxis lets you make a rotation around any axis.

At the point you want to enter orbit you can get the look direction by the cross product between your axis of rotation, and a vector between the ship and the center of rotation. Something like (untested):

var lookDir = Vector3.Cross(Vector3.forward, transform.position - orbitAround.transform.position);

You may have to reverse the parameters to the Vector3.Cross(). If you want to use Transform.LookAt(), you will need to add the base position of your ship to the look direction.

transform.LookAt(lookDir + transform.Position);

This is the direction you would use if you were using Quaternion.SetLookRotation().

transform.rotation = Quaternion.SetLookRotation(lookDir);

You may want to use SetLookRotation() as one step making an eased rotation based on a new look rotation. It depends on how you break and reestablish orbit.

For this to work, you also have to have your ship constructed so that the nose is facing positive ‘Z’. My point above, is that once you’ve established the rotation of the ship, you don’t have to set it each frame. RotateAround() will maintain the correct heading.