Creating a Vector3 towards a different Vector3

Hello,

The title might be a tad vague, so I'll do my best to explain my question.

Consider that I have an object A and an object B. I want to create an object C in between those and give C the rotation so that it's pointing towards object B. C needs to be a certain distance away from A.

E.g.: you could think of C as the Moon. I want to create that Moon in the orbit of the Earth (A) so that it's in between the Earth and the Sun (B).

Oh, and the rotation of A cannot be moved, so I can't try transform.LookAt(B) for A and then create C in front of the rotated A.

Thanks.

Okay, so there's two problems here:

  • placing C in the world
  • making C point to B

the last one is the easiest: `C.transform.LookAt(B.transform);` the first one can be done something like this:

Vector3 delta = B.position - A.position;
C.position = A.position + delta.GetNormalized() * desiredOrbitDistance;

give or take a few compile errors :)

The easiest way is probably to parent both B and C to an empty game object (hereafter called A0) in the center of A. Then you can just point B at C to begin with, and when you rotate A0, B and C will also rotate, but not lose their relationship. If you put B and C along an axis of A0, then it will be easy to change how far away from A/A0 they are, also.