- Home /
Camera Follow Object While Looking At Other Target
I have a sphere object that can freely move around which I can easily follow with the camera. (This is easy) The sphere object needs to be moved to a certain position for the player to earn score. (This is easy) I can follow the sphere with the camera while looking at the target position. (This is easy too.)
Basically I just added a follow script to the camera and targeted the sphere then added LookAt code to the script that makes the camera look at the target position.
The part that I haven't been able to figure out is:
Always have the freely rolling around sphere between the camera and the target position. So the player is able to see the sphere and target at all times. While the sphere is rolling around freely the player has no control over the sphere's movement. (A bit like billiards)
I have added a dummy object inside the sphere to get the rotation of the lookat value of the Y axis and copied that to the camera's Y axis. This helps but the sphere is cant be seen after the ball passes by the target and the angle is too much as there is a set distance between the camera and the sphere.
Any ideas? I am lost to how to calculate the relative position of the camera to the sphere while looking at target.
Answer by robertbu · Mar 19, 2013 at 04:51 PM
Say you have the game objects of the target, the sphere and the camera. You can calculate a position that will keep them in a row as follows:
var v3T = sphere.transform.position - target.transform.position;
camera.transform.position = sphere.transform.position + v3T.normalized * camera_distance;
where camera_distance is the distance you want between the camera and the sphere.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094711im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera follow in c# 1 Answer
Camera to smoothly LookAt() a series of empty objects along a curved path that act as a 'rail' 3 Answers
Making a camera that follows a rigidbodied sphere. 2 Answers
Camera following/looking at aircraft 1 Answer
Can anyone please tell me why this script doesnt work? - Raycasting 1 Answer