Advertisement
Guest User

Player Code

a guest
Feb 2nd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8.     private Rigidbody2D rb;
  9.     private SpriteRenderer sprite;
  10.     private Animator anim;
  11.     private float dirX = 0f;
  12.     [SerializeField] private float moveSpeed = 4f;
  13.     private bool isPunching = false;
  14.     private bool isCrouching = false;
  15.  
  16.     // Start is called before the first frame update
  17.     private void Start()
  18.     {
  19.         rb = GetComponent<Rigidbody2D>();
  20.         sprite = GetComponent<SpriteRenderer>();
  21.         anim = GetComponent<Animator>();
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     private void Update()
  26.     {
  27.         // If currently punching or crouching, disable movement
  28.         if (isPunching || isCrouching)
  29.         {
  30.             dirX = 0f;
  31.         }
  32.         else
  33.         {
  34.             // Allow Movement
  35.             dirX = Input.GetAxisRaw("Horizontal");
  36.         }
  37.  
  38.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
  39.         UpdateAnimationState();
  40.  
  41.         // Punch on spacebar press
  42.         if (Input.GetKeyDown(KeyCode.Space) && !isCrouching)
  43.         {
  44.             Punch();
  45.         }
  46.  
  47.         // Crouch on "S" key down
  48.         if (Input.GetKeyDown(KeyCode.S) && !isPunching)
  49.         {
  50.             Crouch();
  51.         }
  52.         // Stand up on "S" key up
  53.         else if (Input.GetKeyUp(KeyCode.S))
  54.         {
  55.             StandUp();
  56.         }
  57.     }
  58.  
  59.     // Updates animations for player movement
  60.     private void UpdateAnimationState()
  61.     {
  62.         if (dirX != 0f)
  63.         {
  64.             anim.SetBool("walking", true);
  65.             sprite.flipX = dirX < 0f;
  66.         }
  67.         else
  68.         {
  69.             anim.SetBool("walking", false);
  70.         }
  71.     }
  72.  
  73.     private void Punch()
  74.     {
  75.         if (!isCrouching)
  76.         {
  77.             isPunching = true;
  78.             anim.SetTrigger("punch");
  79.         }
  80.     }
  81.  
  82.     private void Crouch()
  83.     {
  84.         if (!isPunching)
  85.         {
  86.             isCrouching = true;
  87.             anim.SetBool("crouch", true);
  88.         }
  89.     }
  90.  
  91.  
  92.     private void StandUp()
  93.     {
  94.         isCrouching = false;
  95.         anim.SetBool("crouch", false);
  96.     }
  97.  
  98.     // Called from animation event to signal the end of punch animation
  99.     public void FinishPunch()
  100.     {
  101.         isPunching = false;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement