UnityGameProjectsCode / Rise2Point0Game / Level / LevelCameraEffects.cs
LevelCameraEffects.cs
Raw
using Cinemachine;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class LevelCameraEffects : MonoBehaviour
{
    //zoom out camera to see full level
    //zoom out camera based on player movement speed
    //shake camera on impact
    //enable or disable camera tracking

    [SerializeField] private float fullViewTransitionDuration, scrollZoomStrength, maxFullViewOrtho = 15;

    //values for zoom transition (speed changes)
    private bool cameraZoomActive = false;
    private float baseZoomOrtho, zoomOrthoAtEffectStart, currentZoomTransitionTime, zoomTransitionStartTime, zoomTransitionDuration, maxZoomOrtho;

    //values used for vignette effect
    private float rbMagnitude;

    //values for full view transition (close or far perspective)
    private bool fullViewActive = false;
    private float baseFullViewOrtho, currentFullViewTransitionTime, fullViewTransitionStartTime;
    private bool fullViewTransitionComplete , fullViewTransitioning;

    [SerializeField] private VolumeProfile postProcessingVolume;
    private CinemachineVirtualCamera vCamera;
    private Rigidbody2D playerRB;
    private GameObject deathWall;

    private void Awake()
    {
        vCamera = GetComponent<CinemachineVirtualCamera>();
        playerRB = ObjectManager.GetObject(0).GetComponent<Rigidbody2D>();
        deathWall = ObjectManager.GetObject(2);

        baseFullViewOrtho = vCamera.m_Lens.OrthographicSize;
        baseZoomOrtho = vCamera.m_Lens.OrthographicSize;
    }

    private void Update()
    {
        if (cameraZoomActive)
        {
            currentZoomTransitionTime = (Time.time - zoomTransitionStartTime) / zoomTransitionDuration;

            float smoothedValue = Mathf.SmoothStep(zoomOrthoAtEffectStart, maxZoomOrtho, currentZoomTransitionTime);

            vCamera.m_Lens.OrthographicSize = smoothedValue;

            if (smoothedValue >= maxZoomOrtho)
            {
                cameraZoomActive = false;
                currentZoomTransitionTime = 0;
                zoomTransitionStartTime = Time.time;
            }
        }

        if (!cameraZoomActive && vCamera.m_Lens.OrthographicSize != baseZoomOrtho && !fullViewActive)
        {
            currentZoomTransitionTime = (Time.time - zoomTransitionStartTime) / zoomTransitionDuration;

            float smoothedValue = Mathf.SmoothStep(maxZoomOrtho, baseZoomOrtho, currentZoomTransitionTime);

            vCamera.m_Lens.OrthographicSize = smoothedValue;

            if (smoothedValue <= baseZoomOrtho)
            {
                currentZoomTransitionTime = 0;
            }
        }

        if (fullViewActive && !fullViewTransitionComplete && fullViewTransitioning)
        {
            currentFullViewTransitionTime = (Time.time - fullViewTransitionStartTime) / fullViewTransitionDuration;

            float smoothedValue = Mathf.SmoothStep(baseFullViewOrtho, maxFullViewOrtho, currentFullViewTransitionTime);

            vCamera.m_Lens.OrthographicSize = smoothedValue;

            if (smoothedValue >= maxFullViewOrtho)
            {
                fullViewTransitionComplete = true;
                fullViewTransitioning = false;
            }
        }

        if (!fullViewActive && fullViewTransitionComplete && fullViewTransitioning)
        {
            currentFullViewTransitionTime = (Time.time - fullViewTransitionStartTime) / fullViewTransitionDuration;

            float smoothedValue = Mathf.SmoothStep(maxFullViewOrtho, baseFullViewOrtho, currentFullViewTransitionTime);

            vCamera.m_Lens.OrthographicSize = smoothedValue;

            if (smoothedValue <= baseFullViewOrtho)
            {
                fullViewTransitionComplete = false;
                fullViewTransitioning = false;
            }
        }

        ApplyVignetteEffect();
    }

    public void ApplyVignetteEffect()
    {
        rbMagnitude = playerRB.velocity.magnitude;

        float vignetteStrength = Mathf.InverseLerp(0, 72, rbMagnitude);

        postProcessingVolume.TryGet(out Vignette postVignette);
        postVignette.smoothness.value = vignetteStrength + 0.225f;
    }

    public void DisableCameraTracking(bool state)
    {
        vCamera.enabled = state;
    }

    public void AdjustCameraPositionForInvertedControl(bool state)
    {
        if (state)
            vCamera.GetCinemachineComponent<CinemachineFramingTransposer>().m_ScreenY = 0.5f;
        else
            vCamera.GetCinemachineComponent<CinemachineFramingTransposer>().m_ScreenY = 0.85f;
    }

    public void ApplyZoomEffect(float zoomIntensity)
    {
        //based on given intensity, determine the values needed to setup the zoom effect.

        if (!fullViewActive && zoomIntensity > 4)
        {
            float zoomTime = 0.0f;
            float zoomStrength = 0.0f;

            for (int i = 0; i < zoomIntensity; i++)
            {
                zoomTime += 0.1f;
                zoomStrength += 0.1f;
            }

            zoomOrthoAtEffectStart = vCamera.m_Lens.OrthographicSize;

            maxZoomOrtho = zoomOrthoAtEffectStart + zoomStrength;
            zoomTransitionDuration = zoomTime;
            zoomTransitionStartTime = Time.time;

            cameraZoomActive = true;
        }
    }

    public void ActivateFullView()
    {
        if (!GUIManager.GetMainMenuCanvasState() && !GUIManager.GetDeathCanvasState() && !GUIManager.GetUnlocksPanelState() && !GUIManager.GetPauseCanvasState())
        {
            if (vCamera.m_Lens.OrthographicSize == baseFullViewOrtho || vCamera.m_Lens.OrthographicSize == maxFullViewOrtho)
            {
                if (!fullViewActive)
                {
                    cameraZoomActive = false;
                    fullViewActive = true;
                    fullViewTransitionComplete = false;
                    currentFullViewTransitionTime = 0;
                    fullViewTransitionStartTime = Time.time;
                    GUIManager.SetScoreParticlesForFullView();
                    fullViewTransitioning = true;
                }
                else
                {
                    fullViewActive = false;
                    fullViewTransitionComplete = true;
                    currentFullViewTransitionTime = 0;
                    fullViewTransitionStartTime = Time.time;
                    GUIManager.SetScoreParticlesForNormalView();
                    fullViewTransitioning = true;
                }
            }
        }
    }

    public void SetZoomLevel(Vector2 scrollValues)
    {
        if (scrollValues.y == -120)
        {
            if (vCamera.m_Lens.OrthographicSize + scrollZoomStrength > 15)
                return;

            vCamera.m_Lens.OrthographicSize += scrollZoomStrength;
        }


        if (scrollValues.y == 120)
        {
            if (vCamera.m_Lens.OrthographicSize - scrollZoomStrength < 7)
                return;

            vCamera.m_Lens.OrthographicSize -= scrollZoomStrength;
        }

    }

    public void StartFollowingDeathWall()
    {
        if (!GUIManager.GetMainMenuCanvasState() && vCamera.Follow == playerRB.transform)
        {
            cameraZoomActive = false;
            fullViewActive = true;
            vCamera.Follow = deathWall.transform;
            GUIManager.SetScoreParticlesForFullView();
        }
    }

    public void StopFollowingDeathWall()
    {
        if (!GUIManager.GetMainMenuCanvasState() && vCamera.Follow == deathWall.transform)
        {
            fullViewActive = false;
            vCamera.Follow = playerRB.transform;
            GUIManager.SetScoreParticlesForNormalView();
        }
    }
}