Rotate object arround other object, but always facing the camera

Hi everyone,

I’m not working long with Unity and i have a problem of how to approach this situation.

What we have in the scene:

  • object1
  • object2
  • camera

What i want to do:

Object1 is a static object, not moving, just sitting there. On the right side of object1 is object2. Object2 should always be next (right side) to object 1. So if the camera moves arround object1, object2 should always rotate arround object1 and from the cameras perspective stay right of object1.

Object2 should also rotate itself to face the camera, but thats not the point of this thread.

Maybe this image can help understanding what i want to do:

Some help would be really nice.
Thanks,
Angelo

So the “object 2” should always be perpedicular to the line between “object 1” and the camera?

Try this script, you need to drag the camera and the center object into the respective variables.

The comments should explain what’s going on.

using UnityEngine;
using System.Collections;

public class Satellite : MonoBehaviour
{
  public Camera _camera;
  public GameObject _center_object;
  public float _distance = 2.0f;

	void Update ()
  {
    // vector from the center-object to the camera
    Vector3 vec = _center_object.transform.position - _camera.transform.position;

    // create a vector that's perpedicular to the up vector *and* the vector towards the camera
    // => vector from the center-object to the new satellite position
    Vector3 vec_sphere = Vector3.Cross( vec, _center_object.transform.up );

    transform.position = _center_object.transform.position - vec_sphere.normalized * _distance;
	}
}