Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour, IDamageable
  6. {
  7. private Rigidbody2D _rigid;
  8. [SerializeField]
  9. private float _jumpForce = 5.0f;
  10. private bool _resetJump = false;
  11. [SerializeField]
  12. private float _speed = 5.0f;
  13. private bool _grounded = false;
  14.  
  15. private PlayerAnimation _playerAnim;
  16. private SpriteRenderer _playerSprite;
  17. private SpriteRenderer _swordArcSprite;
  18.  
  19.  
  20. int IDamageable.Health { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
  21.  
  22. // use this for initializations
  23. void Start()
  24. {
  25. _rigid = GetComponent<Rigidbody2D>();
  26. _playerAnim = GetComponent<PlayerAnimation>();
  27. _playerSprite = GetComponentInChildren<SpriteRenderer>();
  28. _swordArcSprite = transform.GetChild(1).GetComponent<SpriteRenderer>();
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. Movement();
  35.  
  36. if (Input.GetMouseButtonDown(0) && IsGrounded() == true)
  37. _playerAnim.Attack();
  38.  
  39.  
  40. }
  41.  
  42. void Movement()
  43. {
  44. float move = Input.GetAxisRaw("Horizontal");
  45. _grounded = IsGrounded();
  46.  
  47.  
  48. if (move > 0)
  49. {
  50. Flip(true);
  51.  
  52. }
  53. else if (move < 0)
  54. {
  55.  
  56. Flip(false);
  57. }
  58.  
  59.  
  60. if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() == true)
  61. {
  62. Debug.Log("Jump!");
  63. _rigid.velocity = new Vector2(_rigid.velocity.x, _jumpForce);
  64. StartCoroutine(ResetJumpRoutine());
  65. _playerAnim.Jump(true);
  66. }
  67. _rigid.velocity = new Vector2(move * _speed, _rigid.velocity.y);
  68.  
  69. _playerAnim.Move(move);
  70.  
  71.  
  72. }
  73.  
  74. bool IsGrounded()
  75. {
  76. RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.down, 0.3f, 1 << 8);
  77. Debug.DrawRay(transform.position, Vector2.down, Color.green);
  78.  
  79. if (hitInfo.collider != null)
  80. {
  81. if (_resetJump == false)
  82. {
  83. Debug.Log("Grounded");
  84. _playerAnim.Jump(false);
  85. return true;
  86. }
  87.  
  88. }
  89. return false;
  90.  
  91. }
  92.  
  93. void Flip(bool faceRight)
  94. {
  95. if (faceRight == true)
  96. {
  97. _playerSprite.flipX = false;
  98. _swordArcSprite.flipX = false;
  99. _swordArcSprite.flipY = false;
  100.  
  101. Vector3 newPos = _swordArcSprite.transform.localPosition;
  102. newPos.x = 0.767f;
  103. _swordArcSprite.transform.localPosition = newPos;
  104.  
  105.  
  106.  
  107. }
  108. else if (faceRight == false)
  109. {
  110. _playerSprite.flipX = true;
  111. _swordArcSprite.flipX = true;
  112. _swordArcSprite.flipY = true;
  113.  
  114. Vector3 newPos = _swordArcSprite.transform.localPosition;
  115. newPos.x = -0.767f;
  116. _swordArcSprite.transform.localPosition = newPos;
  117. }
  118.  
  119. }
  120.  
  121. IEnumerator ResetJumpRoutine()
  122. {
  123. _resetJump = true;
  124. yield return new WaitForSeconds(0.1f);
  125. _resetJump = false;
  126.  
  127. }
  128. public void Damage()
  129. {
  130. Debug.Log("Player: :Damage()");
  131. }
  132.  
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement