Rotate an object in order to make one of his face to face the camera

Hello all, this is what I’ve got:

How can I rotate the colored object in order to make the green triangle face my camera? I know the object position, the camera position and the center of the green triangle (Vector3 position in world cords)

After a week of headhaches I found a solution:

  • create an empty GameObject at (0, 0, 0) (same as the colored object) and call it “Pivot”
  • use this code

Vector3 goCenter = calculateGameObjectCenter(gameObjectToFace); //the center of the face you want to see in front of the camera

pivot.transform.lookAt(goCenter); //make the empty object point the center of that face

coloredObj.transform.parent = pivot.transform; //make the coloredObj the child of the pivot in order to rotate it with the pivot

pivot.transform.LookAt (camera.transform.position); //make the face, face the camera (use Quaternion.Slerp for a smoother effect)

coloredObj.transform.parent = null; //break the relation between the two objects