Advertisement
DugganSC

Untitled

Dec 22nd, 2023 (edited)
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using Cinemachine;
  4. using Game.Scripts.UI;
  5.  
  6. namespace Game.Scripts.LiveObjects
  7. {
  8.     public class Drone : MonoBehaviour
  9.     {
  10.         private enum Tilt
  11.         {
  12.             NoTilt, Forward, Back, Left, Right
  13.         }
  14.  
  15.         [SerializeField]
  16.         private Rigidbody _rigidbody;
  17.         [SerializeField]
  18.         private float _speed = 5f;
  19.         private bool _inFlightMode = false;
  20.         [SerializeField]
  21.         private Animator _propAnim;
  22.         [SerializeField]
  23.         private CinemachineVirtualCamera _droneCam;
  24.         [SerializeField]
  25.         private InteractableZone _interactableZone;
  26.  
  27.         [SerializeField]
  28.         private Game.Scripts.Player.Player _player;
  29.  
  30.         private PlayerInputs _playerInput;
  31.        
  32.  
  33.         public static event Action OnEnterFlightMode;
  34.         public static event Action OnExitFlightmode;
  35.  
  36.         private void OnEnable()
  37.         {
  38.             SetUpDroneInputs();
  39.             InteractableZone.onZoneInteractionComplete += EnterFlightMode;
  40.         }
  41.  
  42.         private void SetUpDroneInputs()
  43.         {
  44.             _playerInput = new PlayerInputs();
  45.             _playerInput.Drone.Escape.performed += Escape_performed;
  46.         }
  47.  
  48.         private void Escape_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  49.         {
  50.             ExitFlightMode();
  51.         }
  52.  
  53.         private void EnterFlightMode(InteractableZone zone)
  54.         {
  55.             if (_inFlightMode != true && zone.GetZoneID() == 4) // drone Scene
  56.             {
  57.                 _player.DisableWalkMode();
  58.                 _playerInput.Drone.Enable();
  59.                 _propAnim.SetTrigger("StartProps");
  60.                 _droneCam.Priority = 11;
  61.                 _inFlightMode = true;
  62.                 OnEnterFlightMode?.Invoke();
  63.                 UIManager.Instance.DroneView(true);
  64.                 _interactableZone.CompleteTask(4);
  65.             }
  66.         }
  67.  
  68.         private void ExitFlightMode()
  69.         {            
  70.             _droneCam.Priority = 9;
  71.             _inFlightMode = false;
  72.             UIManager.Instance.DroneView(false);
  73.             _playerInput.Drone.Disable();
  74.             _player.ActivateWalkMode();
  75.         }
  76.  
  77.         private void Update()
  78.         {
  79.             if (_inFlightMode)
  80.             {
  81.                 CalculateTilt();
  82.                 CalculateMovementUpdate();
  83.             }
  84.         }
  85.  
  86.         public void EscapeFlightMode()
  87.         {
  88.             _inFlightMode = false;
  89.             OnExitFlightmode?.Invoke();
  90.             ExitFlightMode();
  91.         }
  92.  
  93.         private void FixedUpdate()
  94.         {
  95.             _rigidbody.AddForce(transform.up * (9.81f), ForceMode.Acceleration);
  96.             if (_inFlightMode)
  97.                 CalculateMovementFixedUpdate();
  98.         }
  99.  
  100.         private void CalculateMovementUpdate()
  101.         {
  102.             float rotation = _playerInput.Drone.Rotation.ReadValue<float>(); // goes from -1 to 1
  103.             if (_playerInput.Drone.Rotation.IsPressed())
  104.             {
  105.                 var tempRot = transform.localRotation.eulerAngles;
  106.                 tempRot.y += rotation * _speed / 3;
  107.                 transform.localRotation = Quaternion.Euler(tempRot);
  108.             }
  109.         }
  110.  
  111.         private void CalculateMovementFixedUpdate()
  112.         {
  113.             Vector3 movement = _playerInput.Drone.Move.ReadValue<Vector3>();
  114.             if (Math.Abs(movement.y) > 0f)
  115.             {
  116.                 _rigidbody.AddForce(movement.y * transform.up * _speed, ForceMode.Acceleration);
  117.             }
  118.         }
  119.  
  120.         private void CalculateTilt()
  121.         {
  122.             Vector3 movement = _playerInput.Drone.Move.ReadValue<Vector3>().normalized;
  123.             if (movement.magnitude > 0f)
  124.             {
  125.                 Debug.Log($"Calculate Tilt - Magnitude: {movement.magnitude} Value: {_playerInput.Drone.Move.ReadValue<Vector3>()}");
  126.                 transform.rotation = Quaternion.Euler(30 * movement.x, transform.localRotation.eulerAngles.y, 30 * movement.z);
  127.             }
  128.         }
  129.  
  130.         private void OnDisable()
  131.         {
  132.             InteractableZone.onZoneInteractionComplete -= EnterFlightMode;
  133.         }
  134.     }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement