GaelVanhalst

Linked: AuraCamera

Jan 25th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AuraCamera : MonoBehaviour {
  5.  
  6.     //Spline
  7.     [SerializeField] private SimpleSpline _startSpline = null;
  8.     private SimpleSpline _currentSpline;
  9.  
  10.     //Player
  11.     Player _playerScript;
  12.     bool _playerIn;
  13.  
  14.     //Camera
  15.     public float MoveSpeed = 5.0f;
  16.     private Vector3 _cameraStartPos;
  17.     private bool _activated;
  18.     private bool _waiting;
  19.     private float _deltaOnCurrentSpline;
  20.     private const float SyncDelta =1f;
  21.     private bool _syncSplineAndCameraPos;
  22.     private bool _syncToPath;
  23.     private float _speedBasedOnSplineDistance;
  24.  
  25.     void Start () {
  26.         if (_startSpline != null) return;
  27.         Debug.LogError("Start spline is missing");
  28.         Destroy(this);
  29.     }
  30.  
  31.     //Update when player can trigger AuraCamera
  32.     IEnumerator PlayerInUpdate()
  33.     {
  34.         if (_playerIn) yield break;
  35.         _playerIn = true;
  36.         while (_playerIn)
  37.         {
  38.             CheckForSignal();
  39.             yield return null;
  40.         }
  41.     }
  42.  
  43.     //Check if player want and can use the AuraCamera
  44.     void CheckForSignal()
  45.     {
  46.         if (_activated) return;
  47.         if (!InputManager.InteractDown()) return;
  48.         if (_playerScript.BlockPlayerInput) return;
  49.         _playerScript.BlockPlayerInput = true;
  50.         _activated = true;
  51.         _playerScript.CameraScript.StartAuraCamWhenPossible(this);
  52.     }
  53.  
  54.     //Start AuraCamera
  55.     public void StartFollowingTrack()
  56.     {
  57.         StartCoroutine(FollowTrack());
  58.     }
  59.  
  60.     //Follow track of AuraCamera
  61.     IEnumerator FollowTrack()
  62.     {
  63.         _playerScript.CameraScript.OverWriteCameraMovement = true;
  64.         _playerScript.BlockPlayerInput = true;
  65.         _activated = true;
  66.         _deltaOnCurrentSpline = 0.0f;
  67.         _currentSpline = _startSpline;
  68.         _syncSplineAndCameraPos = true;
  69.         _syncToPath = true;
  70.         _cameraStartPos = _playerScript.CameraScript.transform.position;
  71.         CalculateTheMoveSpeed();
  72.  
  73.         while (!FollowAuraTrack())
  74.         {
  75.             yield return null;
  76.         }
  77.         _playerScript.CameraScript.ResetTransform();
  78.         _playerScript.BlockPlayerInput = false;
  79.         _activated = false;
  80.  
  81.         _playerScript.CameraScript.OverWriteCameraMovement = false;
  82.     }
  83.  
  84.     //Calculate the speed to move over the spline
  85.     void CalculateTheMoveSpeed()
  86.     {
  87.         _speedBasedOnSplineDistance = MoveSpeed/_currentSpline.Length;
  88.     }
  89.  
  90.     //Move the AuraCamera if needed
  91.     bool FollowAuraTrack()
  92.     {
  93.         if (_currentSpline == null) return true;
  94.  
  95.         if (!_waiting)
  96.         {
  97.             MoveOnSpline(Time.deltaTime);
  98.         }
  99.         return false;
  100.     }
  101.  
  102.     //Move the AuraCamera if needed
  103.     void MoveOnSpline(float time)
  104.     {
  105.         _deltaOnCurrentSpline += time*_speedBasedOnSplineDistance;
  106.         if (_deltaOnCurrentSpline >= 1) //Reached end current spline
  107.         {
  108.             if (_syncSplineAndCameraPos && !_syncToPath)
  109.             {
  110.                 //Place camera back to player
  111.                 _playerScript.CameraScript.transform.position = _playerScript.CameraScript.GetPositionCameraShouldBe();
  112.             }
  113.             else
  114.             {
  115.                 _playerScript.CameraScript.Set2DPos(_currentSpline.Parameters.EndPoint);
  116.             }
  117.             _syncSplineAndCameraPos = false;
  118.             StartCoroutine(StayOnSpline());
  119.         }
  120.         else
  121.         {
  122.             var position = _currentSpline.CalculateSplinePoint(_deltaOnCurrentSpline);
  123.             if (_syncSplineAndCameraPos)
  124.             {
  125.                 if (_syncToPath)
  126.                 {
  127.                     if (_deltaOnCurrentSpline < SyncDelta)
  128.                     {
  129.                         var delta = _deltaOnCurrentSpline;
  130.                         delta *= (1/SyncDelta);
  131.                         position = Vector3.Lerp(_cameraStartPos, position, delta);
  132.                     }
  133.                     else
  134.                     {
  135.                         _syncSplineAndCameraPos = false;
  136.                     }
  137.                 }
  138.                 else
  139.                 {
  140.                     if (_deltaOnCurrentSpline > 1-SyncDelta)
  141.                     {
  142.                         var positionToGetTo = _playerScript.CameraScript.GetPositionCameraShouldBe();
  143.                         var delta = _deltaOnCurrentSpline-(1-SyncDelta);
  144.                         delta *= (1 / SyncDelta);
  145.                         position = Vector3.Lerp(position, positionToGetTo, delta);
  146.                     }
  147.                 }
  148.             }
  149.             _playerScript.CameraScript.Set2DPos(position);
  150.         }
  151.     }
  152.  
  153.     //Stand still so the player can clearly see the focus point
  154.     IEnumerator StayOnSpline()
  155.     {
  156.         _waiting = true;
  157.  
  158.         yield return new WaitForSeconds(_currentSpline.WaitTime);
  159.  
  160.         _waiting = false;
  161.         _currentSpline = _currentSpline.NextSpline;
  162.  
  163.         //Check if done
  164.         if (_currentSpline == null) yield break;
  165.  
  166.         //Check if on last spline
  167.         if (_currentSpline.NextSpline == null)
  168.         {
  169.             _syncToPath = false;
  170.             _syncSplineAndCameraPos = true;
  171.         }            
  172.         _deltaOnCurrentSpline = 0.0f;
  173.         CalculateTheMoveSpeed();
  174.     }
  175.  
  176.     //Player enters bounds to trigger AuraCamera
  177.     public void OnTriggerEnter(Collider other)
  178.     {
  179.         var playerScript = other.GetComponent<Player>();
  180.         if(playerScript!=null)
  181.         {
  182.             _playerScript = playerScript;
  183.             StartCoroutine(PlayerInUpdate());
  184.         }
  185.     }
  186.  
  187.     //Player leaves bounds to trigger AuraCamera
  188.     public void OnTriggerExit(Collider other)
  189.     {
  190.         var playerScript = other.GetComponent<Player>();
  191.         if (playerScript != null)
  192.         {
  193.             _playerIn = false;
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment