- Home /
Camera focusing on an object specified in a script
Hello, me again. I have a question for my camera script. Now, currently, my code is like:
var target : GameObject;
function Update () {
target = GetComponent(PlayerCreate).clone;
transform.position.x = target.position.x;
transform.position.y = target.position.y;
}
I have an object specified in my 'PlayerCreate' code in the 'clone' var, and i want that object to be what my camera follows... But I'm not sure how to get it to work...
Comment
Answer by DaveA · Mar 24, 2011 at 11:35 PM
var pc : PlayerCreate = GetComponent(PlayerCreate);
target = pc.clone;
But that should move to Start or in any case after the clone is created. Doing it in Update each time will be slower.
In any case, you also want the camera to track it, so in Update:
Camera.main.transform.LookAt (target.transform);