How do you subtract a number from the Z position each time an object is destroyed?
I need the camera to zoom out -0.05 after each time an object is destroyed. I have a camera script and destroy object script. The camera script is below. Everything works fine other than zooming out. I would think the zoomOut function below would do it by subtracting from the z position, but it's not working. Any help would be appreciated!
//Camera Script
public float xMin = -50f;
public float yMin = -50f;
public float xMax = 50f;
public float yMax = 50f;
private Transform target;
public destroy dotDestroy;
.
void Start(){
target = GameObject.Find("Player").transform;
}
void LateUpdate()
{
transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(target.position.y, yMin, yMax), transform.position.z);
}
public void zoomOut()
{
if (dotDestroy)
{
transform.localPosition -= new Vector3(0, 0, -0.05f);
}
}
Thanks for the reply! It was on perspective, but I just tried it on Orthographic and it still doesn't work. Any other suggestions?
No leave it on perspective because orthographic distance does not change anything, you'd have to change the orthographic size. No idea what's wrong unfortunately.
Answer by Endless_Aftermath · Nov 03, 2016 at 08:21 PM
The problem is that you are subtracting the "z" position to "-.05f" every time you call "zoomOut()". The z will add because of the double negative. Try this instead:
public void zoomOut()
{
if (dotDestroy)
{
transform.localPosition = new Vector3( transform.localPosition.x , transform.localPosition .y, transform.localPosition.z -0.05f);
}
}
AND you might wanna try putting the code into "FixedUpdate()" instead of "LateUpdate()"