Advertisement
DugganSC

Player.cs

Jan 11th, 2024
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     private CharacterController _controller;
  8.     private PlayerControls _playerInputs;
  9.  
  10.     [SerializeField]
  11.     private float _moveSpeed = 5f;
  12.     [SerializeField]
  13.     private float _gravity = 10f;
  14.     [SerializeField]
  15.     private float _jumpHeight = 50f;
  16.    
  17.     private bool _isJumping;
  18.  
  19.     private bool _doubleJumped = false;
  20.  
  21.     [SerializeField]
  22.     private float _yVelocity;
  23.     private bool _isGrounded;
  24.  
  25.     [SerializeField]
  26.     private int _score = 0;
  27.  
  28.     [SerializeField]
  29.     private int _lives = 3;
  30.  
  31.     [SerializeField]
  32.     private float _respawnDepth = -20f;
  33.  
  34.     [SerializeField]
  35.     private Transform _respawnPoint;
  36.    
  37.     [SerializeField]
  38.     private float _airControlMult = 0.1f;
  39.  
  40.     public delegate void OnInteract();
  41.     public static event OnInteract onInteract;
  42.  
  43.     // Start is called before the first frame update
  44.     void Start()
  45.     {        
  46.         if (!TryGetComponent<CharacterController>(out _controller))
  47.         {
  48.             Debug.LogError("No character controller defined");
  49.             Debug.Break();
  50.         }
  51.  
  52.         _controller.enabled = false;
  53.         transform.position = _respawnPoint.position;
  54.         _controller.enabled = true;
  55.  
  56.         _playerInputs = new PlayerControls();
  57.         _playerInputs.Platforming.Enable();
  58.         _playerInputs.Platforming.Jump.performed += Jump_performed;
  59.         _playerInputs.Platforming.Interact.performed += Interact_performed;
  60.  
  61.         if (_respawnPoint == null)
  62.         {
  63.             Debug.Log("No respawn point set, assuming initial position");
  64.             _respawnPoint = transform;
  65.         }
  66.     }
  67.  
  68.     private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  69.     {
  70.         onInteract.Invoke();
  71.     }
  72.  
  73.     public int GetScore() {
  74.         return _score;
  75.     }
  76.  
  77.     private void OnEnable()
  78.     {
  79.         Collectible.onCollect += Collectible_onCollect;
  80.     }
  81.  
  82.     private void OnDisable()
  83.     {
  84.         Collectible.onCollect -= Collectible_onCollect;
  85.     }
  86.  
  87.     private void OnCollisionEnter(Collision collision)
  88.     {
  89.         Debug.Log("Collision Enter");
  90.     }
  91.  
  92.     private void OnCollisionStay(Collision collision)
  93.     {
  94.         Debug.Log("Collided");
  95.         if (!_isGrounded && collision.gameObject.CompareTag("Wall"))
  96.         {
  97.             ContactPoint[] contactPoints = new ContactPoint[collision.contactCount];
  98.             int collisionCount = collision.GetContacts(contactPoints);
  99.             foreach (ContactPoint cp in contactPoints)
  100.             {
  101.                 Debug.DrawRay(cp.point, cp.normal);
  102.             }
  103.         }
  104.     }
  105.  
  106.     private void OnControllerColliderHit(ControllerColliderHit hit)
  107.     {
  108.         if (!_isGrounded)
  109.         {
  110.             Debug.DrawRay(hit.point, hit.normal * 5, Color.green);
  111.         }
  112.     }
  113.  
  114.     private void Collectible_onCollect(int value)
  115.     {
  116.         _score += value;
  117.         UIManager.Instance.UpdateScore(_score);
  118.     }
  119.  
  120.     private void Jump_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  121.     {
  122.         if (_isGrounded || !_doubleJumped)
  123.         {
  124.             _isJumping = true;
  125.         }
  126.     }
  127.  
  128.     private void FixedUpdate()
  129.     {
  130.         _isGrounded = _controller.isGrounded;
  131.     }
  132.  
  133.     // Update is called once per frame
  134.     void Update()
  135.     {
  136.         Vector2 playerMovement = _playerInputs.Platforming.Movement.ReadValue<Vector2>();
  137.         Vector3 velocity = _controller.velocity;
  138.         if (_isGrounded)
  139.         {
  140.             velocity = new Vector3(playerMovement.x, 0, 0) * _moveSpeed;
  141.         }
  142.  
  143.         if (!_isGrounded)
  144.         {
  145.             if (_isJumping && !_doubleJumped)
  146.             {
  147.                 _yVelocity += _jumpHeight;
  148.                 _doubleJumped = true;
  149.                 _isJumping = false;
  150.             }
  151.             _yVelocity -= _gravity;
  152.         }
  153.         else
  154.         {
  155.             if (_isJumping)
  156.             {
  157.                 _yVelocity = _jumpHeight;
  158.                 _isJumping = false;
  159.                 _doubleJumped = false;
  160.             }
  161.             else
  162.             {
  163.                 // _yVelocity = 0;
  164.             }
  165.         }
  166.  
  167.         velocity.y = _yVelocity;
  168.  
  169.         if (velocity.z > 0)
  170.         {
  171.             Debug.Log(velocity);
  172.             Debug.Break();
  173.         }
  174.  
  175.         _controller.Move(velocity * Time.deltaTime);
  176.  
  177.         if (transform.position.y < _respawnDepth)
  178.         {
  179.             Debug.Log("Respawning");
  180.             // Character has fallen and died
  181.             _lives--;
  182.             UIManager.Instance.UpdateLives(_lives);
  183.  
  184.             if (_lives <= 0)
  185.             {
  186.                 // Game Over
  187.                 SceneManager.LoadScene("Level1");
  188.             }
  189.  
  190.             _controller.enabled = false;
  191.             transform.position = _respawnPoint.position;
  192.             _controller.enabled = true;
  193.         }
  194.     }
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement