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)
I forgot to say that I want to make it possible for all the other faces aswell (e.g. the blue triangle on the top, the yellow one on the left, and with the non-visible on the opposite side too). I tried with Quaternion.LookRotation(camera.transform.position - faceCenter); but it keeps spinning the whole object on the y axis.
Answer by HUBA-GAMES.Sixx93 · May 21, 2016 at 01:49 PM
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