Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class AuraCamera : MonoBehaviour {
- //Spline
- [SerializeField] private SimpleSpline _startSpline = null;
- private SimpleSpline _currentSpline;
- //Player
- Player _playerScript;
- bool _playerIn;
- //Camera
- public float MoveSpeed = 5.0f;
- private Vector3 _cameraStartPos;
- private bool _activated;
- private bool _waiting;
- private float _deltaOnCurrentSpline;
- private const float SyncDelta =1f;
- private bool _syncSplineAndCameraPos;
- private bool _syncToPath;
- private float _speedBasedOnSplineDistance;
- void Start () {
- if (_startSpline != null) return;
- Debug.LogError("Start spline is missing");
- Destroy(this);
- }
- //Update when player can trigger AuraCamera
- IEnumerator PlayerInUpdate()
- {
- if (_playerIn) yield break;
- _playerIn = true;
- while (_playerIn)
- {
- CheckForSignal();
- yield return null;
- }
- }
- //Check if player want and can use the AuraCamera
- void CheckForSignal()
- {
- if (_activated) return;
- if (!InputManager.InteractDown()) return;
- if (_playerScript.BlockPlayerInput) return;
- _playerScript.BlockPlayerInput = true;
- _activated = true;
- _playerScript.CameraScript.StartAuraCamWhenPossible(this);
- }
- //Start AuraCamera
- public void StartFollowingTrack()
- {
- StartCoroutine(FollowTrack());
- }
- //Follow track of AuraCamera
- IEnumerator FollowTrack()
- {
- _playerScript.CameraScript.OverWriteCameraMovement = true;
- _playerScript.BlockPlayerInput = true;
- _activated = true;
- _deltaOnCurrentSpline = 0.0f;
- _currentSpline = _startSpline;
- _syncSplineAndCameraPos = true;
- _syncToPath = true;
- _cameraStartPos = _playerScript.CameraScript.transform.position;
- CalculateTheMoveSpeed();
- while (!FollowAuraTrack())
- {
- yield return null;
- }
- _playerScript.CameraScript.ResetTransform();
- _playerScript.BlockPlayerInput = false;
- _activated = false;
- _playerScript.CameraScript.OverWriteCameraMovement = false;
- }
- //Calculate the speed to move over the spline
- void CalculateTheMoveSpeed()
- {
- _speedBasedOnSplineDistance = MoveSpeed/_currentSpline.Length;
- }
- //Move the AuraCamera if needed
- bool FollowAuraTrack()
- {
- if (_currentSpline == null) return true;
- if (!_waiting)
- {
- MoveOnSpline(Time.deltaTime);
- }
- return false;
- }
- //Move the AuraCamera if needed
- void MoveOnSpline(float time)
- {
- _deltaOnCurrentSpline += time*_speedBasedOnSplineDistance;
- if (_deltaOnCurrentSpline >= 1) //Reached end current spline
- {
- if (_syncSplineAndCameraPos && !_syncToPath)
- {
- //Place camera back to player
- _playerScript.CameraScript.transform.position = _playerScript.CameraScript.GetPositionCameraShouldBe();
- }
- else
- {
- _playerScript.CameraScript.Set2DPos(_currentSpline.Parameters.EndPoint);
- }
- _syncSplineAndCameraPos = false;
- StartCoroutine(StayOnSpline());
- }
- else
- {
- var position = _currentSpline.CalculateSplinePoint(_deltaOnCurrentSpline);
- if (_syncSplineAndCameraPos)
- {
- if (_syncToPath)
- {
- if (_deltaOnCurrentSpline < SyncDelta)
- {
- var delta = _deltaOnCurrentSpline;
- delta *= (1/SyncDelta);
- position = Vector3.Lerp(_cameraStartPos, position, delta);
- }
- else
- {
- _syncSplineAndCameraPos = false;
- }
- }
- else
- {
- if (_deltaOnCurrentSpline > 1-SyncDelta)
- {
- var positionToGetTo = _playerScript.CameraScript.GetPositionCameraShouldBe();
- var delta = _deltaOnCurrentSpline-(1-SyncDelta);
- delta *= (1 / SyncDelta);
- position = Vector3.Lerp(position, positionToGetTo, delta);
- }
- }
- }
- _playerScript.CameraScript.Set2DPos(position);
- }
- }
- //Stand still so the player can clearly see the focus point
- IEnumerator StayOnSpline()
- {
- _waiting = true;
- yield return new WaitForSeconds(_currentSpline.WaitTime);
- _waiting = false;
- _currentSpline = _currentSpline.NextSpline;
- //Check if done
- if (_currentSpline == null) yield break;
- //Check if on last spline
- if (_currentSpline.NextSpline == null)
- {
- _syncToPath = false;
- _syncSplineAndCameraPos = true;
- }
- _deltaOnCurrentSpline = 0.0f;
- CalculateTheMoveSpeed();
- }
- //Player enters bounds to trigger AuraCamera
- public void OnTriggerEnter(Collider other)
- {
- var playerScript = other.GetComponent<Player>();
- if(playerScript!=null)
- {
- _playerScript = playerScript;
- StartCoroutine(PlayerInUpdate());
- }
- }
- //Player leaves bounds to trigger AuraCamera
- public void OnTriggerExit(Collider other)
- {
- var playerScript = other.GetComponent<Player>();
- if (playerScript != null)
- {
- _playerIn = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment