- Home /
zooming camera out then back in
hey all, im creating a feature so at the start of the level the camera zooms out to show the map then zooms back in. so far my camera zooms out however when its coded to zoom back in, it does nothing, it just stops zooming altogether. its orthographic btw so im changing size. im using camera.orthographicSize += Time.deltaTime * speedZoom;
to zoom out and zooming in by swicthing the - to a +.
heres my full script
using UnityEngine;
using System.Collections;
public class IsometricCamera : MonoBehaviour
{
private GameObject target;
public GameObject TargettoBall;
public float speedZoom = 9;
public float size = 10;
private float EndZoom = 15;
public float scrollSpeed = 30;
private float maxPos = 9;
private bool StartLevelMove = true;
private bool Zoomedout = false;
// zoom in-out buttons
public GUITexture ZoomIN;
public GUITexture ZoomOUT;
//public Texture2D ZoomIN;
//public Texture2D ZoomOUT;
private Camera cam;
void Start()
{
this.cam = (Camera)this.gameObject.GetComponent("Camera");
this.cam.isOrthoGraphic = true;
this.cam.transform.rotation = Quaternion.Euler(42, 36, 359);
this.cam.orthographicSize = 9;
target = TargettoBall;
StartCoroutine(Changecam());
speedZoom = 9;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (ZoomIN.HitTest(Input.mousePosition))
{
if (this.cam.orthographicSize >=6)
this.cam.orthographicSize -= 2;
Debug.Log (this.cam.orthographicSize);
//ZoomIn();
}
if (ZoomOUT.HitTest(Input.mousePosition))
{
if (this.cam.orthographicSize <=14)
this.cam.orthographicSize += 2;
Debug.Log (this.cam.orthographicSize);
//ZoomOut();
}
}
if (StartLevelMove == true)
{
camera.orthographicSize += Time.deltaTime * speedZoom;
}
//}
// void LateUpdate()
//
// {
//this.cam.orthographicSize -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed * Time.deltaTime;
float distance = 30;
var go = target.gameObject;
if (go == null)
{
}
else
{
transform.position = Vector3.Lerp(transform.position, target.transform.position + new Vector3(-distance, distance, -distance), 2.0f * Time.deltaTime);
this.cam.transform.LookAt(target.transform);
}
if (camera.orthographicSize <= maxPos)
speedZoom = 0;
Debug.Log (speedZoom);
}
IEnumerator Changecam()
{
yield return new WaitForSeconds(2.5f);
camera.orthographicSize -= Time.deltaTime * speedZoom;
StartLevelMove = false;
//speedZoom = 9;
//this.cam.orthographicSize = 9;
}
}
Answer by Freaking-Pingo · Nov 03, 2013 at 10:25 PM
After 2.5 seconds, your code executes the remaining part of the coroutine. The problem is, this is only executed once because you don't have any loops An example could be:
IEnumerator Changecam()
{
yield return new WaitForSeconds(2.5f);
StartLevelMove = false;
while(camera.orthographicSize > 6){
camera.orthographicSize -= Time.deltaTime * speedZoom;
yield return null;
}
}
Remember to save your project when playing with while loops :)
yeah sorry about the mess =S
line 24, i set call and set it to true, it zooms out fine when it starts, its getting it to zoom back in where the problem is
Ahh sorry, hold on I know what the problem is then, I'll edit my answer
hmmm, nope still doesnt make it start zoo$$anonymous$$g, i think your right with nthe fact its not in update though, ill try a few things
Your answer
![](https://koobas.hobune.stream/wayback/20220613121203im_/https://answers.unity.com/themes/thub/images/avi.jpg)