- Home /
Camera follow instantiated object and reset
Hello all!
I'm currently coming up against what I'm assuming is a noob-ish problem, but i'm an artist primarily so there ya go.
I've set up a script on my camera to follow an instantiated object and snap back to it's original position when the object is destroyed. Here is the code:
#pragma strict
var smooth : float = 3;
var zOffset : float = 5000;
var planetFollow : boolean = false;
var FollowOn : boolean = false;
private var shootObject : GameObject;
private var planetScript : fire_planet;
private var planetGone : setShotTrue;
private var planetBorder : GameObject;
var myCamPos : Vector3;
function Start () {
shootObject = GameObject.FindGameObjectWithTag("launcher");
planetScript = shootObject.GetComponent(fire_planet);
planetBorder = GameObject.FindGameObjectWithTag("blackHole");
planetGone = shootObject.GetComponent(setShotTrue);
myCamPos = transform.position;
}
function Update () {
if (planetScript.planetShot == true){
Follow();
FollowOn = true;
}
}
function Follow () {
if (FollowOn == true){
yield WaitForSeconds(2);
var planetObject = GameObject.FindGameObjectWithTag("Player");
var planetPos : Vector3 = planetObject.transform.position;
var targetPos : Vector3 = Vector3( planetPos.x, planetPos.y, planetPos.z + zOffset);
transform.position = Vector3.Lerp ( transform.position, targetPos, smooth );
transform.LookAt( planetObject.transform );
}
else if (FollowOn == false){
transform.position = myCamPos;
}
}
However, despite having the dampening effect in the code, the camera just instantly snaps to the instantiated object (hence the yield WaitForSeconds) without any smoothing.
The other issue seems to be that once the object is destroyed and my trigger volume sets FollowOn to False, I'm met with a null reference exception because the object being followed is being destroyed.
How do I get around these issues? If anyone has any feedback I would greatly appreciate it.
Thanks!