Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public enum PlayerState
- {
- walk,
- attack,
- interact,
- stagger,
- idle
- }
- public class PlayerController : MonoBehaviour
- {
- public PlayerState currentState;
- public float speed;
- private Rigidbody2D myRigidbody;
- private Vector3 change;
- private Animator animator;
- public FloatValue currentHealth;
- public CustomSignal playerHealthSignal;
- // Start is called before the first frame update
- void Start()
- {
- currentState = PlayerState.walk;
- animator = GetComponent<Animator>();
- myRigidbody = GetComponent<Rigidbody2D>();
- animator.SetFloat("moveX", 0);
- animator.SetFloat("moveY", -1);
- }
- // Update is called once per frame
- void Update()
- {
- change = Vector3.zero;
- change.x = Input.GetAxisRaw("Horizontal");
- change.y = Input.GetAxisRaw("Vertical");
- if (Input.GetButtonDown("Attack") && currentState != PlayerState.attack
- && currentState != PlayerState.stagger)
- {
- StartCoroutine(AttackCo());
- }
- else if (currentState == PlayerState.walk || currentState == PlayerState.idle)
- {
- UpdateAnimationAndMove();
- }
- }
- private IEnumerator AttackCo()
- {
- animator.SetBool("attacking", true);
- currentState = PlayerState.attack;
- yield return null;
- animator.SetBool("attacking", false);
- yield return new WaitForSeconds(.33f);
- currentState = PlayerState.walk;
- }
- void UpdateAnimationAndMove()
- {
- if (change != Vector3.zero)
- {
- MoveCharacter();
- animator.SetFloat("moveX", change.x);
- animator.SetFloat("moveY", change.y);
- animator.SetBool("moving", true);
- }
- else
- animator.SetBool("moving", false);
- }
- void MoveCharacter()
- {
- change.Normalize();
- myRigidbody.MovePosition(transform.position + change * speed * Time.deltaTime);
- }
- public void Knock(float knockTime, float damage)
- {
- currentHealth.RunTimeValue -= damage;
- playerHealthSignal.Raise();
- if (currentHealth.RunTimeValue > 0)
- {
- StartCoroutine(KnockCo(knockTime));
- }
- else
- {
- this.gameObject.SetActive(false);
- }
- }
- private IEnumerator KnockCo(float knockTime)
- {
- if (myRigidbody != null)
- {
- yield return new WaitForSeconds(knockTime);
- myRigidbody.velocity = Vector2.zero;
- currentState = PlayerState.idle;
- myRigidbody.velocity = Vector2.zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement