- Home /
Camera Following Spawned Object
I have found some sample code which is of a pinball machine and it's basic mechanics.
The thing that I am trying to do is get the camera to follow the ball, but the ball isn't spawned until the game is run, and every time the ball falls between the flippers, a new ball is spawned.
Can anyone show me how to set the camera to follow a specific object if it exists, and if it doesn't, set the camera to another default position.
Thanks in advance!
Answer by Berenger · Feb 19, 2012 at 01:57 AM
When the ball is instantiated, tell the camera to follow it. It depends how your doing that though. By parenting (Camera.main.transform.parent = ball.transform) ? By script (Camera.main.GetComponent(FollowScript).target = ball) ?
Answer by JamieHickman · Feb 19, 2012 at 06:00 PM
Thanks for the response. The ball is crated within a Java Script called Player.
The code for creating the ball looks like this:
// Releases a ball into the playfield
function ReleaseBall() : Ball {
// Now create a new ball
var newball : GameObject;
newball = GameObject.Instantiate(ballPrefab, Vector3(62.70164, -45.26075, 0),
Quaternion.identity);
newball.transform.localScale = ballPrefab.transform.localScale;
newball.name = "Ball";
// Update our ball array so other objects can quickly do a seek on all balls
UpdateBallArray();
return newball.GetComponent("Ball");
}