Advertisement
Guest User

MovementScriptHelp

a guest
Jun 5th, 2024
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement2 : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     private float walkSpeed = 5f;
  9.  
  10.     Animator animator;
  11.     private string currentState;
  12.  
  13.     private float yAxis;
  14.     private float xAxis;
  15.     private Rigidbody2D rb2d;
  16.     private bool isJumpPressed;
  17.     private float jumpForce = 5;
  18.     private int groundMask;
  19.     private bool isGrounded;
  20.     private string currentAnimation;
  21.     private bool isAttackPressed;
  22.     private bool isAttacking;
  23.  
  24.     [SerializeField]
  25.     private float attackDelay = 0.3f;
  26.  
  27.     //Animation states
  28.     const string IDLE = "IDLE";
  29.     const string Jump = "Jump";
  30.     const string BASIC_ATTACK = "Basic_attack";
  31.     const string JUMP_ATTACK = "Jump_attack";
  32.     const string fall = "fall";
  33.     const string WALK = "WALK";
  34.  
  35.  
  36.  
  37.     // Start is called before the first frame update
  38.     void Start()
  39.     {
  40.         rb2d = GetComponent<Rigidbody2D>();
  41.         animator = GetComponent<Animator>();
  42.         groundMask = 1 << LayerMask.NameToLayer("Ground");
  43.     }
  44.  
  45.     void ChangeAnimationState(string newState)
  46.     {
  47.         if (currentState == newState) return;
  48.  
  49.         //play the animation'
  50.         animator.Play(newState);
  51.  
  52.         //reassign the current state
  53.         currentState = newState;
  54.     }
  55.  
  56.     // Update is called once per frame
  57.     void Update()
  58.     {
  59.         //Checking for inputs
  60.         xAxis = Input.GetAxisRaw("Horizontal");
  61.  
  62.         //space jump key pressed?
  63.         if (Input.GetKeyDown(KeyCode.Space))
  64.         {
  65.             isJumpPressed = true;
  66.         }
  67.  
  68.         if (Input.GetMouseButtonDown(0))
  69.         {
  70.             isAttackPressed = true;
  71.         }
  72.     }
  73.  
  74.     //Physics based time step loop
  75.     private void FixedUpdate()
  76.     {
  77.         //check if player is on the ground
  78.         RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 0.1f, groundMask);
  79.  
  80.         if (hit.collider != null)
  81.         {
  82.             isGrounded = true;
  83.  
  84.         }
  85.         else
  86.         {
  87.             isGrounded = false;
  88.         }
  89.  
  90.         //--------------------------------------------------
  91.  
  92.         //Check update movement based on input
  93.         Vector2 vel = new Vector2(0, rb2d.velocity.y);
  94.  
  95.         if (xAxis < 0)
  96.         {
  97.             vel.x = -walkSpeed;
  98.             transform.localScale = new Vector2(-1, 1);
  99.         }
  100.         else if (xAxis > 0)
  101.         {
  102.             vel.x = walkSpeed;
  103.             transform.localScale = new Vector2(1, 1);
  104.         }
  105.         else
  106.         {
  107.             vel.x = 0;
  108.         }
  109.  
  110.         if (isGrounded && !isAttacking)
  111.         {
  112.             if (xAxis != 0)
  113.             {
  114.                 ChangeAnimationState(WALK);
  115.             }
  116.             else
  117.             {
  118.                 ChangeAnimationState(IDLE);
  119.             }
  120.         }
  121.  
  122.         //-------------------------------------
  123.  
  124.         //Check if trying to jump
  125.         if (isJumpPressed && isGrounded)
  126.         {
  127.             rb2d.AddForce(new Vector2(0, jumpForce));
  128.             isJumpPressed = false;
  129.             ChangeAnimationState(Jump);
  130.         }
  131.  
  132.         //assign the new velocity to the rigidbody
  133.         rb2d.velocity = vel;
  134.  
  135.         //attack
  136.         if (isAttackPressed)
  137.         {
  138.             isAttackPressed = false;
  139.  
  140.             if (!isAttacking)
  141.             {
  142.                 isAttacking = true;
  143.  
  144.                 if (isGrounded)
  145.                 {
  146.                     ChangeAnimationState(BASIC_ATTACK);
  147.                 }
  148.                 else
  149.                 {
  150.                     ChangeAnimationState(JUMP_ATTACK);
  151.                 }
  152.  
  153.                 attackDelay = animator.GetCurrentAnimatorStateInfo(0).length;
  154.                 Invoke("AttackComplete", attackDelay);
  155.             }
  156.  
  157.         }
  158.  
  159.     }
  160.  
  161.     void AttackComplete()
  162.     {
  163.         isAttacking = false;
  164.     }
  165.  
  166. }
  167.  
  168.     //Animation manager
  169.  
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement