- Home /
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:
But what i was aiming for was more like this:
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 :)
Place the ship in a correct position at the start, then remove the LookAt().
But how will then the object rotation change when it is in a different point on its orbit, since the ship is orbiting the planet?
Just try it. For a simple orbit as you have here, it will work. If you decide to allow your ship to make arbitrary orbits around the planet, then you will have some work to do.
The thing is that the ship is able to break orbit and head for another planet/object, in which he can get into orbit again when he gets there. So i must be able to calculate the rotation offset at any given time. Which i am trying to figure out how to do. That or i didn't understand your suggestion :)
Answer by robertbu · Apr 01, 2013 at 04:32 PM
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.
Again, thank you for helping me. Thanks to everyone else too, but Robert's answer got me the in the correct direction :)
transform.LookAt(this.owner.transform.position, Vector3.back); transform.Rotate(0, 90, 0);
This code got me the desired results. I also modified some of my code since i did changes to rotation on every update frame, which was obviously wrong, since i needed to do it only once. Thanks for clearing this one too Robert!
Answer by instruct9r · Apr 01, 2013 at 02:44 PM
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 :)
Answer by Owen-Reynolds · Apr 01, 2013 at 03:16 PM
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.