Advertisement
Guest User

Playerscript

a guest
Nov 11th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class Player : MonoBehaviour
  7. {
  8.  
  9. [SerializeField]
  10. public float speed;
  11. private float moveInput;
  12.  
  13. private Rigidbody2D rb;
  14. [SerializeField]
  15. public float jumpForce;
  16.  
  17. private bool facingRight = true;
  18.  
  19. private bool _resetJump = false;
  20. private bool _grounded = false;
  21.  
  22.  
  23. public Transform groundCheck;
  24. public float checkRadius;
  25. public LayerMask whatIsGround;
  26.  
  27. private int extraJumps;
  28. public int extraJumpsValue;
  29.  
  30. private PlayerAnimation _playerAnim;
  31. private SpriteRenderer _playerSprite;
  32. Animator m_Animator;
  33. bool m_Jump;
  34.  
  35.  
  36.  
  37.  
  38. void Start()
  39. {
  40. extraJumps = extraJumpsValue;
  41. rb = GetComponent<Rigidbody2D>();
  42. _playerAnim = GetComponent<PlayerAnimation>();
  43. _playerSprite = GetComponentInChildren<SpriteRenderer>();
  44. m_Animator = GetComponentInChildren<Animator>();
  45. m_Jump = false;
  46.  
  47. }
  48.  
  49. void FixedUpdate()
  50. {
  51. _grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
  52.  
  53.  
  54.  
  55. moveInput = Input.GetAxis("Horizontal");
  56.  
  57. rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  58.  
  59. if (facingRight == false && moveInput > 0)
  60. {
  61. Flip();
  62. }
  63. else if (facingRight == true && moveInput < 0)
  64. {
  65. Flip();
  66. }
  67. _playerAnim.Move(moveInput);
  68.  
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75. void Update()
  76. {
  77.  
  78. if (_grounded == true)
  79. {
  80. extraJumps = extraJumpsValue;
  81. m_Jump = true;
  82.  
  83.  
  84. }
  85.  
  86.  
  87. if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
  88. {
  89. rb.velocity = Vector2.up * jumpForce;
  90. extraJumps--;
  91.  
  92.  
  93. if (m_Jump == true)
  94. m_Animator.SetBool("Jumping", true);
  95. Debug.Log("playing jumping animation");
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. }
  103. // i think here somewhere is the bug...
  104. else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && _grounded == true)
  105. {
  106. rb.velocity = Vector2.up * jumpForce;
  107. rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  108. m_Animator.SetBool("Jumping", false);
  109. Debug.Log("I am Jumping");
  110.  
  111.  
  112. }
  113.  
  114.  
  115. }
  116.  
  117.  
  118. void Flip()
  119. {
  120. facingRight = !facingRight;
  121. Vector3 Scaler = transform.localScale;
  122. Scaler.x *= -1;
  123. transform.localScale = Scaler;
  124.  
  125. }
  126.  
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement