Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement2 : MonoBehaviour
- {
- [SerializeField]
- private float walkSpeed = 5f;
- Animator animator;
- private string currentState;
- private float yAxis;
- private float xAxis;
- private Rigidbody2D rb2d;
- private bool isJumpPressed;
- private float jumpForce = 5;
- private int groundMask;
- private bool isGrounded;
- private string currentAnimation;
- private bool isAttackPressed;
- private bool isAttacking;
- [SerializeField]
- private float attackDelay = 0.3f;
- //Animation states
- const string IDLE = "IDLE";
- const string Jump = "Jump";
- const string BASIC_ATTACK = "Basic_attack";
- const string JUMP_ATTACK = "Jump_attack";
- const string fall = "fall";
- const string WALK = "WALK";
- // Start is called before the first frame update
- void Start()
- {
- rb2d = GetComponent<Rigidbody2D>();
- animator = GetComponent<Animator>();
- groundMask = 1 << LayerMask.NameToLayer("Ground");
- }
- void ChangeAnimationState(string newState)
- {
- if (currentState == newState) return;
- //play the animation'
- animator.Play(newState);
- //reassign the current state
- currentState = newState;
- }
- // Update is called once per frame
- void Update()
- {
- //Checking for inputs
- xAxis = Input.GetAxisRaw("Horizontal");
- //space jump key pressed?
- if (Input.GetKeyDown(KeyCode.Space))
- {
- isJumpPressed = true;
- }
- if (Input.GetMouseButtonDown(0))
- {
- isAttackPressed = true;
- }
- }
- //Physics based time step loop
- private void FixedUpdate()
- {
- //check if player is on the ground
- RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 0.1f, groundMask);
- if (hit.collider != null)
- {
- isGrounded = true;
- }
- else
- {
- isGrounded = false;
- }
- //--------------------------------------------------
- //Check update movement based on input
- Vector2 vel = new Vector2(0, rb2d.velocity.y);
- if (xAxis < 0)
- {
- vel.x = -walkSpeed;
- transform.localScale = new Vector2(-1, 1);
- }
- else if (xAxis > 0)
- {
- vel.x = walkSpeed;
- transform.localScale = new Vector2(1, 1);
- }
- else
- {
- vel.x = 0;
- }
- if (isGrounded && !isAttacking)
- {
- if (xAxis != 0)
- {
- ChangeAnimationState(WALK);
- }
- else
- {
- ChangeAnimationState(IDLE);
- }
- }
- //-------------------------------------
- //Check if trying to jump
- if (isJumpPressed && isGrounded)
- {
- rb2d.AddForce(new Vector2(0, jumpForce));
- isJumpPressed = false;
- ChangeAnimationState(Jump);
- }
- //assign the new velocity to the rigidbody
- rb2d.velocity = vel;
- //attack
- if (isAttackPressed)
- {
- isAttackPressed = false;
- if (!isAttacking)
- {
- isAttacking = true;
- if (isGrounded)
- {
- ChangeAnimationState(BASIC_ATTACK);
- }
- else
- {
- ChangeAnimationState(JUMP_ATTACK);
- }
- attackDelay = animator.GetCurrentAnimatorStateInfo(0).length;
- Invoke("AttackComplete", attackDelay);
- }
- }
- }
- void AttackComplete()
- {
- isAttacking = false;
- }
- }
- //Animation manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement