Advertisement
TGM_XLANDC

Unity 2D Player Controller

Jan 10th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5.     //Components
  6.     private Rigidbody2D rb;
  7.     private Animator animator;
  8.     private SpriteRenderer spriteRenderer;
  9.  
  10.     private float horizontalAxis;
  11.  
  12.     [SerializeField]
  13.     private float moveSpeed;
  14.     [SerializeField]
  15.     private float jumpForce;
  16.     [SerializeField]
  17.     private int jumpCount;
  18.     [SerializeField]
  19.     private int maxJumpCount;
  20.     // Start is called once before the first execution of Update after the MonoBehaviour is created
  21.     void Start()
  22.     {
  23.         //Get components
  24.         rb = GetComponent<Rigidbody2D>();
  25.         animator = GetComponent<Animator>();
  26.         spriteRenderer = GetComponent<SpriteRenderer>();
  27.  
  28.         //Default values
  29.         jumpCount = 0;
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update()
  34.     {
  35.         ControlsUpdate();
  36.     }
  37.  
  38.     private void FixedUpdate()
  39.     {
  40.         ControlsFixedUpdate();
  41.     }
  42.  
  43.     private void ControlsUpdate()
  44.     {
  45.         //Get horizontal axis (horizontal movement direction) (-1 if A is pressed, 0 when nothing is pressed, 1 when D is pressed
  46.         horizontalAxis = Input.GetAxis("Horizontal");
  47.         //If player is moving
  48.         if (horizontalAxis != 0)
  49.         {
  50.             //Enable running animation
  51.             animator.SetBool("isRunning", true);
  52.             //Flip sprite
  53.             if (horizontalAxis > 0 && !spriteRenderer.flipX)
  54.                 spriteRenderer.flipX = true;
  55.             else if (horizontalAxis < 0 && spriteRenderer.flipX)
  56.                 spriteRenderer.flipX = false;
  57.         }
  58.         //If player is not moving, disable running animation
  59.         else
  60.             animator.SetBool("isRunning", false);
  61.        
  62.         //If player can jump
  63.         if (jumpCount < maxJumpCount)
  64.         {
  65.             //Check if jump keys are pressed
  66.             if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.Space))
  67.             {
  68.                 //Modify liniear velocity (Y axis)
  69.                 rb.linearVelocityY = jumpForce;
  70.                 //For older versions of Unity
  71.                 //rb.velocity = new Vector2(rb.velocity.x,jumpForce);
  72.  
  73.  
  74.                 //Increase jumpCount by 1
  75.                 jumpCount++;
  76.                 //Enable jumping animation
  77.                 animator.SetInteger("jumpCount", jumpCount);
  78.             }
  79.         }
  80.  
  81.     }
  82.  
  83.     private void ControlsFixedUpdate()
  84.     {
  85.         rb.linearVelocityX = moveSpeed * horizontalAxis;
  86.         //For old Unity
  87.         //rb.velocity = new Vector2(moveSpeed * horizontalAxis, rb.velocity.y)
  88.     }
  89.  
  90.     private void OnCollisionEnter2D(Collision2D collision)
  91.     {
  92.         //Reset jump count on collision with platforms
  93.         if (collision.collider.CompareTag("Platform"))
  94.         {
  95.             jumpCount = 0;
  96.             animator.SetInteger("jumpCount", jumpCount);
  97.         }
  98.     }
  99. }
  100.  
Tags: C# Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement