Camera registering it is at correct position when it isn't
I am creating a program to move from one planet to another when a button is clicked.
When this button is clicked it passed the planet name into this function:
public void moveToPlanet(string planet)
{
switch (planet)
{
case "Earth2":
offset = new Vector3(0.8f, 0.2f, 0f);
break;
case "Jupiter2":
offset = new Vector3(5.0f, 0.2f, 0);
break;
default:
break;
}
target = solarsys.FindChild("Ss_rotating planets2").transform.FindChild(planet).GetChild(0);
startPosition = cam.transform.position;
shouldMove = true;
atTarget = false;
}
then it runs through update to move the camera, first click on a planet is fine:
void Update () {
if (shouldMove == true)
{
Debug.Log(shouldMove);
Debug.Log(target.name);
Debug.Log(cam.transform.position);
Debug.Log(target.position);
cam.transform.position = Vector3.Lerp(startPosition, (target.position + offset),speed);
cam.transform.LookAt(target.position);
Debug.Log(target.position + offset);
Debug.Log(cam.transform.position);
if(cam.transform.position == (target.position + offset))
{
atTarget = true;
}
else
{
atTarget = false;
}
}
if (atTarget == true)
{
shouldMove = false;
cam.transform.LookAt(target.position);
Vector3 targetMovement = GetTargetMovement();
UpdatePosition(targetMovement);
}
}
then when I click another planet, it sets the wrong target position so the camera is in the incorrect place and I'm not sure why? If you see any silly boolean errors or stupid mistakes please point them out!
The strangest thing is, when I click the same button again like this order:
Earth->Jupiter->Jupiter then Earth will work, first Jupiter click is in wrong position but 2nd Jupiter click is in the correct position. I'm stumped.
Comment