Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public enum PlayerState
  6. {
  7. walk,
  8. attack,
  9. interact,
  10. stagger,
  11. idle
  12. }
  13.  
  14. public class PlayerController : MonoBehaviour
  15. {
  16. public PlayerState currentState;
  17. public float speed;
  18. private Rigidbody2D myRigidbody;
  19. private Vector3 change;
  20. private Animator animator;
  21. public FloatValue currentHealth;
  22. public CustomSignal playerHealthSignal;
  23.  
  24. // Start is called before the first frame update
  25. void Start()
  26. {
  27. currentState = PlayerState.walk;
  28. animator = GetComponent<Animator>();
  29. myRigidbody = GetComponent<Rigidbody2D>();
  30. animator.SetFloat("moveX", 0);
  31. animator.SetFloat("moveY", -1);
  32.  
  33. }
  34.  
  35. // Update is called once per frame
  36. void Update()
  37. {
  38. change = Vector3.zero;
  39. change.x = Input.GetAxisRaw("Horizontal");
  40. change.y = Input.GetAxisRaw("Vertical");
  41. if (Input.GetButtonDown("Attack") && currentState != PlayerState.attack
  42. && currentState != PlayerState.stagger)
  43. {
  44. StartCoroutine(AttackCo());
  45. }
  46. else if (currentState == PlayerState.walk || currentState == PlayerState.idle)
  47. {
  48. UpdateAnimationAndMove();
  49. }
  50.  
  51. }
  52.  
  53. private IEnumerator AttackCo()
  54. {
  55. animator.SetBool("attacking", true);
  56. currentState = PlayerState.attack;
  57. yield return null;
  58. animator.SetBool("attacking", false);
  59. yield return new WaitForSeconds(.33f);
  60. currentState = PlayerState.walk;
  61. }
  62.  
  63. void UpdateAnimationAndMove()
  64. {
  65. if (change != Vector3.zero)
  66. {
  67. MoveCharacter();
  68. animator.SetFloat("moveX", change.x);
  69. animator.SetFloat("moveY", change.y);
  70. animator.SetBool("moving", true);
  71. }
  72. else
  73. animator.SetBool("moving", false);
  74. }
  75.  
  76. void MoveCharacter()
  77. {
  78. change.Normalize();
  79. myRigidbody.MovePosition(transform.position + change * speed * Time.deltaTime);
  80. }
  81.  
  82. public void Knock(float knockTime, float damage)
  83. {
  84.  
  85. currentHealth.RunTimeValue -= damage;
  86. playerHealthSignal.Raise();
  87. if (currentHealth.RunTimeValue > 0)
  88. {
  89.  
  90. StartCoroutine(KnockCo(knockTime));
  91. }
  92. else
  93. {
  94. this.gameObject.SetActive(false);
  95. }
  96.  
  97. }
  98.  
  99. private IEnumerator KnockCo(float knockTime)
  100. {
  101. if (myRigidbody != null)
  102. {
  103. yield return new WaitForSeconds(knockTime);
  104. myRigidbody.velocity = Vector2.zero;
  105. currentState = PlayerState.idle;
  106. myRigidbody.velocity = Vector2.zero;
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement