Advertisement
NovusX

ledgegrab

Jul 11th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.94 KB | None | 0 0
  1.  
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerLedgeChecker : MonoBehaviour
  7. {
  8.     [SerializeField] Transform _handPosition, _standPosition;
  9.     [SerializeField] private float yOffset = 6.5f;
  10.  
  11.     private Vector3 newHandPos;
  12.  
  13.     private void Start()
  14.     {
  15.         newHandPos = new Vector3(_handPosition.position.x, _handPosition.position.y - yOffset, _handPosition.position.z);
  16.     }
  17.  
  18.     private void OnTriggerEnter(Collider other)
  19.     {
  20.         if (other.CompareTag("Ledge Checker"))
  21.         {
  22.             var player = other.GetComponentInParent<Player>();
  23.             player.GrabLedge(newHandPos, this);
  24.         }
  25.     }
  26.  
  27.     public Vector3 GetStandUpPos()
  28.     {
  29.         return _standPosition.position;
  30.     }
  31.  
  32. }//class
  33.  
  34.  
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using UnityEngine;
  38. using Cinemachine;
  39.  
  40. public class Player : MonoBehaviour
  41. {
  42.     [SerializeField] private float _moveSpeed;
  43.     [SerializeField] private float _ladderClimbSpeed = 6f;
  44.     [SerializeField] private float _jumpHeight;
  45.     [SerializeField] private float _gravity = -1f;
  46.     [SerializeField] private float _pushForce = 6f;
  47.  
  48.     private Vector3 _moveDirection;
  49.     private Vector3 _velocity;
  50.     private float zHorizontal, yVertical;
  51.     private float _yVelocity;
  52.     private int _coins;
  53.     private bool _isJumping;
  54.     private bool _grabbedLedge;
  55.     private bool _canClimbLadder;
  56.     private bool _onLadder;
  57.     private bool _isPushing;
  58.     private bool _isRolling = false;
  59.     private bool _isFrozen;
  60.  
  61.     private enum State { Normal, Rolling, Freeze}
  62.     private State _state;
  63.  
  64.     PlayerLedgeChecker _activeLedge;
  65.     LadderLedgeClimb _activeLadder;
  66.  
  67.     #region Properties
  68.     public float Velocity => _velocity.x;
  69.     public bool IsJumping => _isJumping;
  70.     public bool CanClimbLadder
  71.     {
  72.         get => _canClimbLadder;
  73.         set => _canClimbLadder = value;
  74.     }
  75.  
  76.     public bool GrabbedLedge
  77.     {
  78.         get => _grabbedLedge;
  79.         set => _grabbedLedge = value;
  80.     }
  81.  
  82.     public bool OnLadder
  83.     {
  84.         get => _onLadder;
  85.         set => _onLadder = value;
  86.     }
  87.     public bool IsPushing
  88.     {
  89.         get => _isPushing;
  90.         set => _isPushing = value;
  91.     }
  92.  
  93.     public bool IsFrozen => _isFrozen;
  94.  
  95.  
  96.     #endregion
  97.  
  98.  
  99.     CharacterController _controller;
  100.  
  101.     private void Awake()
  102.     {
  103.         _controller = GetComponent<CharacterController>();    
  104.     }
  105.  
  106.  
  107.     void Update()
  108.     {
  109.         zHorizontal = Input.GetAxisRaw("Horizontal");
  110.         yVertical = Input.GetAxisRaw("Vertical");
  111.        
  112.         if (_grabbedLedge) return;
  113.         switch (_state)
  114.         {
  115.             case State.Normal:
  116.                 _isFrozen = false;
  117.                 CalculateMovement();
  118.                 FlipPlayer();
  119.                 DodgeRollInput();
  120.                 break;
  121.  
  122.             case State.Rolling:
  123.                 _isFrozen = false;
  124.                 HandleRolling();
  125.                 break;
  126.             case State.Freeze:
  127.                 _isFrozen = true;
  128.                 FreezePlayer();
  129.                 break;
  130.  
  131.         }
  132.  
  133.         if (Input.GetKeyUp(KeyCode.E) && _isPushing)
  134.         {
  135.             _isPushing = false;
  136.         }
  137.     }
  138.  
  139.     private void CalculateMovement()
  140.     {
  141.         if (_controller.isGrounded)
  142.         {
  143.  
  144.             if (_isJumping)
  145.             {
  146.                 _isJumping = false;
  147.             }
  148.  
  149.             if (_onLadder)
  150.             {
  151.                 _onLadder = false;
  152.             }
  153.  
  154.             _moveDirection.z = zHorizontal;
  155.             _velocity = _moveDirection * _moveSpeed;
  156.  
  157.             if (Input.GetKeyDown(KeyCode.Space))
  158.             {
  159.                 _isJumping = true;
  160.                // _yVelocity = 0;
  161.                 _yVelocity = _jumpHeight;
  162.             }
  163.  
  164.             //Ladder
  165.             if (Input.GetKeyDown(KeyCode.W) && _canClimbLadder)
  166.             {
  167.                 _onLadder = true;
  168.                 _canClimbLadder = false;
  169.             }
  170.  
  171.         }
  172.         else
  173.         {
  174.             if (!OnLadder)
  175.             {
  176.                 _yVelocity += _gravity;
  177.             }
  178.         }
  179.  
  180.         //Ladder
  181.         if (_onLadder)
  182.         {
  183.             _yVelocity = 0;
  184.             _moveDirection.y = yVertical;
  185.             _velocity = _moveDirection * _ladderClimbSpeed;
  186.         }
  187.  
  188.         if(!_onLadder) _velocity.y = _yVelocity;
  189.  
  190.         _controller.Move(_velocity * Time.deltaTime);
  191.     }
  192.  
  193.     private void FlipPlayer()
  194.     {
  195.         if (_onLadder || _grabbedLedge) return;
  196.        
  197.         if (zHorizontal > 0)
  198.         {
  199.             transform.localScale = new Vector3(3, 3, 3);
  200.         }
  201.         else if (zHorizontal < 0)
  202.         {
  203.             transform.localScale = new Vector3(3, 3, -3);
  204.         }
  205.        
  206.     }
  207.  
  208.     private void DodgeRollInput()
  209.     {
  210.         if (Input.GetKeyDown(KeyCode.LeftShift))
  211.         {
  212.             _state = State.Rolling;
  213.         }
  214.     }
  215.  
  216.     private void HandleRolling()
  217.     {
  218.         if(!_isRolling)
  219.             GetComponentInChildren<PlayerAnimation>().DodgePlayer();
  220.  
  221.         var dir = _moveSpeed;
  222.  
  223.         if (transform.localScale.z > 0)
  224.         {
  225.             if (dir < 0) dir *= -1;
  226.         }
  227.         else
  228.         {
  229.             if (dir > 0) dir *= -1;
  230.         }
  231.         _controller.SimpleMove(new Vector3(0,0, dir));
  232.         _isRolling = true;
  233.     }
  234.  
  235.     public void ChangeStateToNormal()
  236.     {
  237.         _state = State.Normal;
  238.         _isRolling = false;
  239.  
  240.         if (_controller.enabled == false)
  241.         {
  242.             _controller.enabled = true;
  243.         }
  244.     }
  245.  
  246.     public void ChangeStateToFreeze()
  247.     {
  248.         _state = State.Freeze;
  249.     }
  250.  
  251.     private void FreezePlayer()
  252.     {
  253.         _controller.enabled = false;
  254.     }
  255.  
  256.     private void OnControllerColliderHit(ControllerColliderHit hit)
  257.     {
  258.         if (hit.collider.CompareTag("Crate_Pushable"))
  259.         {
  260.             var movableObj = hit.collider.attachedRigidbody;
  261.             if (movableObj.isKinematic) return;
  262.             if (hit.moveDirection.y < -0.3f) return;
  263.  
  264.             if (Input.GetKey(KeyCode.E))
  265.             {
  266.                 _isPushing = true;
  267.                 var pushDir = new Vector3(0, 0, hit.moveDirection.z);
  268.                 movableObj.velocity = pushDir * _pushForce;
  269.             }
  270.             else
  271.             {
  272.                 _isPushing = false;
  273.             }
  274.         }
  275.     }
  276.  
  277.     public void GrabLedge(Vector3 handPos, PlayerLedgeChecker currentLedge)
  278.     {
  279.         _controller.enabled = false;
  280.         _grabbedLedge = true;
  281.         transform.position = handPos;
  282.         _isJumping = false;
  283.         _activeLedge = currentLedge;
  284.     }
  285.  
  286.     public void ClimbUpFromLedge()
  287.     {
  288.         _grabbedLedge = false;
  289.         transform.position = _activeLedge.GetStandUpPos();
  290.         _controller.enabled = true;
  291.     }
  292.  
  293.     public void ClimbUpFromLadderAnimationStart(LadderLedgeClimb currentLadder)
  294.     {
  295.         _controller.enabled = false;
  296.         GetComponentInChildren<PlayerAnimation>().ClimbUpFromLadder();
  297.         _activeLadder = currentLadder;
  298.     }
  299.  
  300.     public void ClimbUpFromLadder()
  301.     {
  302.         _onLadder = false;
  303.         transform.position = _activeLadder.GetStandUpPoint();
  304.         _controller.enabled = true;
  305.     }
  306.  
  307.     public void AddCoins()
  308.     {
  309.         _coins++;
  310.         UIManager.Instance.UpdateCoins();
  311.     }
  312.  
  313.     public int GetTotalCoins()
  314.     {
  315.         return _coins;
  316.     }
  317.  
  318.     public void RespawnPlayer(Transform respawnPos)
  319.     {
  320.         StartCoroutine(RespawnPlayerRoutine(respawnPos));
  321.     }
  322.  
  323.     public IEnumerator RespawnPlayerRoutine(Transform respawnPos)
  324.     {
  325.        
  326.         yield return UIManager.Instance.FadeIn(0.5f);
  327.         ChangeStateToFreeze();
  328.         yield return new WaitForSeconds(0.5f);
  329.         transform.position = respawnPos.position;
  330.         yield return UIManager.Instance.FadeOut(2f);
  331.         ChangeStateToNormal();
  332.     }
  333.  
  334. }//class
  335.  
  336.  
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement