Daniel Lip
Daniel Lip

Reputation: 11319

How can I scale a camera slowly and smooth down?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Switch : MonoBehaviour
{
    public GameObject[] cameras;
    public RenderTexture guiCameraRenderTexture;
    public LockSystem lockSystem;
    public GameObject objectToScale;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            lockSystem.PlayerLockState(true, true);
            lockSystem.mouseCursorLockState = false;

            StartCoroutine(scaleOverTime(objectToScale, 3f));
        }
    }

    public IEnumerator scaleOverTime(GameObject objectoToScale, float time)
    {
        float i = 0;
        float rate = 1 / time;

        Vector3 fromScale = objectoToScale.transform.localScale;
        Vector3 toScale = new Vector3(0.1f,0.1f,0.1f);
        while (i < 1)
        {
            i += Time.deltaTime * rate;
            objectoToScale.transform.localScale = Vector3.Lerp(fromScale, toScale, i);
            yield return 0;
        }
    }
}

The objectToScale is the camera GameObject not the Camera component but the GameObject th Camera is component on. And the GameObject is also child of another GameObject.

Maybe I need to scale down the Camera component it self ? Now it's not working it does nothing.

I can see now that the GameObject the Camera is attached to is scale down to 0.1 but in fact it's not changing anything on the Camera the Camera is still all over the screen full size. I want to scale it down to like a window mode size.

What I want to do is a effect when the user press escape to the main menu the camera will squeeze/scale down to this :

Main Menu

This screenshot is from the editor but I mean the bottom one the game view. This is the main menu. I want when the user hit escape it will scale down to this and when he will press continue button for example or escape again it will scale up resize back.

I did using rawimage that in the main menu it's showing live the player camera when the user press the escape key. Now I want to add this effect.

Upvotes: 0

Views: 263

Answers (2)

gameDev_Unity
gameDev_Unity

Reputation: 371

The Transform.localScale or Scale doesn't effect camera component. If you want to adjust the view of Camera, you should use the properties given in the camera component.
I think adjusting FieldOfView can fulfill your requirement.

More on https://docs.unity3d.com/Manual/class-Camera.html

Edited

@DanielLip When you use canvas render mode as screen space - overlay, it draws above every thing in your screen and is totally independent of camera position, scale, FOV, rectsize etc. So you need to use camera rendermode to world space as shown in the picture below. enter image description here

and the scale effect can be achieved by just changing the position of your camera. Like in my case i added 50 units in x and about 50 in z.See Picture: enter image description here

Upvotes: 1

Power EngineTM
Power EngineTM

Reputation: 3

you are facing one problem that i just made for a project with isometric view, maybe this can be useful, this the controller for my camera. The camera don't need to be child of your player or target, only need the transform reference of the target (parent attribute) once you attached this to your camera, you can play around with the values to change the relative position of the camera to the player and focus distance, also you can make callbacks when the focus is done or undone. make me know if there is something else i can do.

public delegate void VoidCallback();

public class CameraController : MonoBehaviour {
    public Camera camera;

    public Transform parent;

    public Vector3 outset;

    public float defaultSize = 10;

    public float focusDistance = 5;

    public float zoomSpeed = 1;

    public bool focused;

    private bool zoomIn;

    private bool zoomOut;

    private VoidCallback focusCallback;

    private VoidCallback enlargeCallback;

    void Update() {
        if (focused) {
            SetInDefaultPosition();
        }
        else {
            if (zoomIn) {
                camera.orthographicSize -= zoomSpeed * Time.deltaTime;
                if (camera.orthographicSize <= focusDistance) {
                    camera.orthographicSize = focusDistance;
                    zoomIn = false;
                    focused = true;
                    if (focusCallback != null) {
                        focusCallback();
                        focusCallback = null;
                    }
                }
            }
            if (zoomOut) {
                camera.orthographicSize += zoomSpeed * Time.deltaTime;
                if (camera.orthographicSize >= defaultSize) {
                    camera.orthographicSize = defaultSize;
                    zoomOut = false;
                    if (enlargeCallback != null) {
                        enlargeCallback();
                        enlargeCallback = null;
                    }
                }
            }
        }
    }

    public void Focus() {
        zoomIn = true;
        zoomOut = false;
    }

    public void Focus(VoidCallback callback) {
        focusCallback = callback;
        Focus();
    }

    public void Enlarge() {
        focused = false;
        zoomIn = false;
        zoomOut = true;
    }

    public void Enlarge(VoidCallback callback) {
        enlargeCallback = callback;
        Enlarge();
    }

    public void ResetSize() {
        focused = false;
        zoomIn = false;
        zoomOut = false;
        camera.orthographicSize = defaultSize;
    }

    public void SetInFocusSize() {
        camera.orthographicSize = focusDistance;
        focused = true;
        zoomIn = false;
        zoomOut = false;
    }

    public void SetInDefaultPosition() {
        transform.position = parent.position + outset;
    }
}

Upvotes: 0

Related Questions