Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.  
  8. private Rigidbody2D _rigid;
  9. [SerializeField]
  10. private float _jumpForce = 5.0f;
  11. private bool _resetJump = false;
  12. [SerializeField]
  13. private float _speed = 5.0f;
  14.  
  15. private PlayerAnimation _anim;
  16.  
  17. void Start()
  18. {
  19.  
  20. _rigid = GetComponent<Rigidbody2D>();
  21. _anim = GetComponent<PlayerAnimation>();
  22.  
  23. }
  24.  
  25. void Update()
  26. {
  27. Movement();
  28. }
  29.  
  30. void Movement()
  31. {
  32.  
  33. float move = Input.GetAxisRaw("Horizontal");
  34.  
  35.  
  36. if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() == true)
  37. {
  38. Debug.Log("Jump!");
  39. _rigid.velocity = new Vector2(_rigid.velocity.x, _jumpForce);
  40. StartCoroutine(ResetJumpRoutine());
  41. }
  42.  
  43. _rigid.velocity = new Vector2(move * _speed, _rigid.velocity.y);
  44.  
  45. _anim.Move(move);
  46. }
  47.  
  48. bool IsGrounded()
  49. {
  50. RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.down * 0.6f, 1 << 8);
  51. if (hitinfo.collider != null)
  52. {
  53. if (_resetJump == false)
  54. return true;
  55. }
  56.  
  57. return false;
  58. }
  59.  
  60. IEnumerator ResetJumpRoutine()
  61. {
  62. _resetJump = true;
  63. yield return new WaitForSeconds(0.1f);
  64. _resetJump = false;
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement