Rotate object 1 to face object 2's velocity

Hey guys, I’m getting lost in the vector/quat/euler differences throughout Unity’s API. Seems like every method relating to rotation uses a different input and output types and while I know my vector math very well, I have a long way to go with quat/eulers.

I have an arrow on a HUD that I want to always point to the direction the ship (a Rigidbody) is moving. So I want arrow.rotation to equal ship.velocity.normalized…or something like that. Quaternion.FromToRotation() seems to be what I want…but its signature is “FromToRotation(Vector3 fromDirection, Vector3 toDirection)”. I can get the toDirection easily, as RB.velocity is a simple vector, but I’m not sure how to get the fromDirection. My arrow.rotation is a quaternion, which I can only get back as a quat or Euler.

So, 1) how can I do what I want? I’d like to have the arrow always display my ship’s flight direction. 2) What’s a good resource for finding more out about interchanging between vectors and quat/eulers? It’s a problem I’ve run into a few times and just haven’t wrapped my head around. Thanks!

If you align the arrow to the Z axis on the GameObject it is attached to then you can simply use:

transform.LookAt( myShipsTransform.position);

Generally a Quaternion represents an orientation, absolute or relative.

You can multiply a Quaternion by a Vector3 to effectively “bend” that vector to your liking.

For instance, Vector3.forward is the unit vector pointing forward.

Quaternion.Euler( 10, 0, 0) * Vector3.forward will give you the same forward vector, but “rotated slightly forward and down by 10 degrees” from the perspective of behind it, and that works in pretty much any direction.

That’s not going to work as that will just point my arrow at the direction my ship is currently pointing, which I already know as I’m piloting the ship :). But thanks for the reply.

Took an hour nap, dreamed about making my game the whole time, came back to it and did much better this time :). This seems to be exactly, or at least very close to, what I want:

Quaternion spaceshipDir = Quaternion.LookRotation(spaceship.GetComponent<Rigidbody>().velocity.normalized);
vectorArrow.transform.rotation = Quaternion.Slerp(vectorArrow.transform.rotation, spaceshipDir, Time.deltaTime);

Maneuvering though the X,Z plane (just yawing left/right) the arrow shows my velocity vector perfectly. If I accellerate forward, then pitch end-over end to point in the reverse direction, the arrow does a near-180° spin when pointing nearly straight up or down. I expected this, and it works just fine for now, but is there any way to keep it from doing that spin? Visually it’s a little distracting. If not, if I model an arrow to be symmetrical around it’s Z axis like a cylinder then a user won’t be able to tell its doing that. That will be an acceptable scenario. This is just a tech demo arrow I slapped together :smile:.