UnityGameProjectsCode / NightwatchGame / PlayerEndMovement.cs
PlayerEndMovement.cs
Raw
using Cinemachine;
using UnityEngine;

public class PlayerEndMovement : MonoBehaviour
{
    //at the end of the game move the player into position and disable all player control related scripts

    private PlayerController pControl;
    private CharacterController charControl;
    private CameraZoom camZoom;
    private PlayerLookingAt pLookingAt;
    private Animator anim;
    private CinemachineVirtualCamera vCam;
    public GameObject creditsCanvas;
    public GameObject gameplayInteractText;
    public Computer comp;
    public GameObject gameplayCenterDot;
    public NoiseSettings perlinNoisePreset;
    public float movementSpeed;
    private bool inPosition;
    private bool startAnimation;

    // Transforms to act as start and end markers for the journey.
    private Transform startPosition;
    public Transform endMarker;

    // Time when the movement started.
    private float startTime;

    // Total distance between the markers.
    private float journeyLength;

    private void Awake()
    {
        pControl = GetComponent<PlayerController>();
        charControl = GetComponent<CharacterController>();
        camZoom = GetComponentInChildren<CameraZoom>();
        pLookingAt = GetComponentInChildren<PlayerLookingAt>();
        anim = GetComponent<Animator>();
        vCam = GetComponentInChildren<CinemachineVirtualCamera>();

        startPosition = transform;
    }

    private void Start()
    {
        pControl.SetFlashlightActive(false);
        pControl.enabled = false;
        charControl.enabled = false;
        camZoom.enabled = false;
        pLookingAt.enabled = false;
        gameplayCenterDot.SetActive(false);
        gameplayInteractText.SetActive(false);
        vCam.m_Lens.FieldOfView = 75;

        // Keep a note of the time the movement started.
        startTime = Time.time;

        // Calculate the journey length.
        journeyLength = Vector3.Distance(startPosition.position, endMarker.position);
    }

    private void Update()
    {
        if (!inPosition)
            MoveIntoPosition();

        //play the animation
        if (inPosition && anim.GetCurrentAnimatorStateInfo(0).IsName("Idle") && startAnimation)
        {
            anim.Play("End Anim");
        }
    }

    public void MoveIntoPosition()
    {
        //move from the players start position to the marker pos
        //called when class is made active for the first time

        // Distance moved equals elapsed time times speed..
        float distCovered = (Time.time - startTime) * movementSpeed;

        // Fraction of journey completed equals current distance divided by total distance.
        float fractionOfJourney = distCovered / journeyLength;

        // Set our position as a fraction of the distance between the markers.
        transform.position = Vector3.Lerp(startPosition.position, endMarker.position, fractionOfJourney);

        float distance = Vector3.Distance(transform.position, endMarker.position);

        if (distance <= 0.1f)
        {
            inPosition = true;
        }
    }

    private void TargetScreenCinemachine()
    {
        //get cinemachine virtual camera and attach a hard look at component to the Aim
        vCam.AddCinemachineComponent<CinemachineHardLookAt>();
    }

    private void AddCinemachineShake()
    {
        vCam.AddCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
        vCam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>().m_NoiseProfile = perlinNoisePreset;
        vCam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>().m_AmplitudeGain = 1.4f;
        vCam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>().m_FrequencyGain = 0.3f;
    }

    private void ActivateCreditsCanvas()
    {
        creditsCanvas.SetActive(true);
    }

    public void SetStartAnimation()
    {
        startAnimation = true;
    }

    private void CrackComputerScreen()
    {
        comp.CrackScreen();
    }
}