1
\$\begingroup\$

I'm zooming on a map using Math.Lerp and then I use the scene manager to change the scene. Here is my attempt but it doesn't work on the map.

I have attached it to the main camera, in the worldView:

public class zoom : MonoBehaviour {

    // Use this for initialization
    private bool zoomedIn = false;
    int zooom = 20;
    float smooth = 5f;
    void Start () {
        SceneManager.LoadScene("worldView");
    }
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            zoomedIn = !zoomedIn;
        }
        if (zoomedIn == true) {
            Camera.current.fieldOfView = Mathf.Lerp (Camera.current.fieldOfView, zooom, Time.deltaTime*smooth);
            SceneManager.LoadScene("countryView");
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ In the same frame that you start zooming in, you immediately load the new scene. Looks like you might want to start a Coroutine to zoom in, yielding over multiple frames to give time to show the effect, before loading the scene at the end. If anyone feels like fleshing out an answer in this vein, feel free — I'm on my phone just now and typing isn't the greatest. \$\endgroup\$
    – DMGregory
    Jun 21, 2016 at 4:40
  • \$\begingroup\$ First of all you are loading a Scene right in your Start(). What do you expect that to do? Second, you are saying it doesn't work. Why doesn't it work? Is it because it loads another scene instead of zooming at all? Third, even if you did remove the Start() -- When you click, what do you expect will happen? Don't you think that country view scene would immediately load i the same frame you press your mouse button? \$\endgroup\$
    – jgallant
    Jun 21, 2016 at 11:14
  • \$\begingroup\$ I also noticed you asked this question at least 3 times. \$\endgroup\$
    – jgallant
    Jun 21, 2016 at 11:30

2 Answers 2

1
\$\begingroup\$

Your script causes a LoadScene to occur on startup, which means the scene will load, and then immediately after loading it will try to load the same scene again, resetting it to the starting state again.

Ignoring that issue, you're not giving the camera script enough time to zoom the camera in. You're loading a new scene on the same frame that you start modifying the camera field of view. You need some timer so the camera has time to zoom in.

If your intent is for this script to persist between scene changes, you need to tell the gameobject not to destroy on load: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

\$\endgroup\$
0
\$\begingroup\$

By some magical way I found out that your camera is orthographic. You don't use Camera.current.fieldOfView for orthographic cameras, instead use Camera.main.orthographicSize. Adjust the line in the second if condition to be

Camera.main.orthographicSize = Mathf.Lerp (Camera.main.orthographicSize, zooom, Time.deltaTime*smooth);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .