Advertisement
JeCodeLeSoir

Player 2D

Apr 21st, 2024 (edited)
804
0
347 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. //version 1.0 🤣
  2.  
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody2D))]
  6. [RequireComponent(typeof(CircleCollider2D))]
  7. public class Player : MonoBehaviour
  8. {
  9.     [Header("Player Physics Settings")]
  10.     [SerializeField] Rigidbody2D m_rigidbody2D;
  11.     [SerializeField] float m_speed = 5.0f;
  12.     [SerializeField] float m_jumpForce = 5.0f;
  13.  
  14.     [Header("Player Grounded Settings")]
  15.  
  16.     [SerializeField] LayerMask layerMask;
  17.  
  18.     [SerializeField] float distanceA = 0.7f;
  19.     [SerializeField] float distanceB = 0.9f;
  20.     [SerializeField] float distanceC = 0.9f;
  21.  
  22.     [Header("Player Debug")]
  23.  
  24.     [SerializeField] float horizontal;
  25.     [SerializeField] Vector2 direction;
  26.     [SerializeField] Vector2 lastVelocity;
  27.     [SerializeField] bool isGrounded;
  28.     [SerializeField] bool isJump;
  29.  
  30.     private Transform tx;
  31.     private RaycastHit2D hit;
  32.  
  33.     private void Reset()
  34.     {
  35.         m_rigidbody2D = GetComponent<Rigidbody2D>();
  36.     }
  37.  
  38.     private void Start()
  39.     {
  40.         tx = this.transform;
  41.         direction = new Vector2(0, 0);
  42.     }
  43.  
  44.     private void FixedUpdate()
  45.     {
  46.         isGrounded = IsGrounded();
  47.  
  48.         if (!isGrounded)
  49.             return;
  50.  
  51.         Move();
  52.         Jump();
  53.     }
  54.  
  55.     private void Move()
  56.     {
  57.         horizontal = Input.GetAxis("Horizontal");
  58.         lastVelocity = m_rigidbody2D.velocity;
  59.        
  60.         direction.x = horizontal;
  61.         direction.y = 0;
  62.  
  63.         lastVelocity.x = 0;
  64.  
  65.         m_rigidbody2D.velocity = lastVelocity + direction * m_speed;
  66.     }
  67.  
  68.     private void ResetJump()
  69.     {
  70.         isJump = false;
  71.     }
  72.  
  73.     private void Jump()
  74.     {
  75.         if (isJump)
  76.             return;
  77.  
  78.         if (!Input.GetKey(KeyCode.Space))
  79.             return;
  80.  
  81.         m_rigidbody2D.AddForce(Vector2.up * m_jumpForce, ForceMode2D.Impulse);
  82.        
  83.         isJump = true;
  84.  
  85.         this.Invoke(nameof(ResetJump), 0.5f);
  86.  
  87.     }
  88.      
  89.     private void OnDrawGizmos()
  90.     {
  91.         if (!tx)
  92.             return;
  93.  
  94.         Debug.DrawRay(tx.position, Vector2.down * distanceA, Color.red);
  95.         Debug.DrawRay(tx.position, (Vector2.down + Vector2.left).normalized * distanceB, Color.red);
  96.         Debug.DrawRay(tx.position, (Vector2.down + Vector2.right).normalized * distanceC, Color.red);
  97.     }
  98.  
  99.    
  100.    
  101.     bool IsGrounded()
  102.     {
  103.         hit = Physics2D.Raycast(tx.position, Vector2.down , distanceA, layerMask);
  104.        
  105.         if (hit.collider != null)
  106.             return true;
  107.  
  108.         hit =
  109.             Physics2D.Raycast(tx.position, (Vector2.down + Vector2.left), distanceB, layerMask);
  110.  
  111.         if (hit.collider != null)
  112.             return true;
  113.        
  114.         hit =
  115.             Physics2D.Raycast(tx.position, Vector2.down + Vector2.right, distanceC, layerMask);
  116.  
  117.         if (hit.collider != null)
  118.             return true;
  119.  
  120.         return false;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement