- Home /
Center camera on object in unity3d by programatically
i wanted to center my camera on object once they are turn is active.. But, i am unable to do it.
Here is my code:
public GameObject UserPlayerPrefab;
public Vector3 v3;
void Update()
{
transform.LookAt(UserPlayerPrefab);
}
i already tried that, but it gave me the error, said that i have to use Vector3. When i change the code to this one:
void Update()
{
transform.LookAt(v3);
}
it works, but not the camera is not center on the object however it is center on the Tile (which is the Plane).
How do i fix it? Thanks
Answer by Professor Snake · Nov 21, 2013 at 04:39 PM
If you don't initialize v3, its default values are (0,0,0) which will make your camera look at that position. Use transform.LookAt(UserPlayerPrefab.transform.position);
i already did it sir, but it is not working. Here is my camera on the game looks like when i not added transform.LookAt(UserPlayerPrefab.transform.position)
:
This is the image when i applied that transform.LookAt(UserPlayerPrefab.transform.position) on
Update` function :
Thank you sir!
Then perhaps that variable doesn't point to the object at the scene. Create a new GameObject variable and drag your player object from the scene objects list to that variable on the script. Then, use that variable for your LookAt function.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Show the GUI button when mouse hover to the object 1 Answer
How to get A Camera Collision? 1 Answer
On Mouse Click Button event 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers