Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- [SerializeField]
- public float speed;
- private float moveInput;
- private Rigidbody2D rb;
- [SerializeField]
- public float jumpForce;
- private bool facingRight = true;
- private bool _resetJump = false;
- private bool _grounded = false;
- public Transform groundCheck;
- public float checkRadius;
- public LayerMask whatIsGround;
- private int extraJumps;
- public int extraJumpsValue;
- private PlayerAnimation _playerAnim;
- private SpriteRenderer _playerSprite;
- Animator m_Animator;
- bool m_Jump;
- void Start()
- {
- extraJumps = extraJumpsValue;
- rb = GetComponent<Rigidbody2D>();
- _playerAnim = GetComponent<PlayerAnimation>();
- _playerSprite = GetComponentInChildren<SpriteRenderer>();
- m_Animator = GetComponentInChildren<Animator>();
- m_Jump = false;
- }
- void FixedUpdate()
- {
- _grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
- moveInput = Input.GetAxis("Horizontal");
- rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
- if (facingRight == false && moveInput > 0)
- {
- Flip();
- }
- else if (facingRight == true && moveInput < 0)
- {
- Flip();
- }
- _playerAnim.Move(moveInput);
- }
- void Update()
- {
- if (_grounded == true)
- {
- extraJumps = extraJumpsValue;
- m_Jump = true;
- }
- if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
- {
- rb.velocity = Vector2.up * jumpForce;
- extraJumps--;
- if (m_Jump == true)
- m_Animator.SetBool("Jumping", true);
- Debug.Log("playing jumping animation");
- }
- // i think here somewhere is the bug...
- else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && _grounded == true)
- {
- rb.velocity = Vector2.up * jumpForce;
- rb.velocity = new Vector2(rb.velocity.x, jumpForce);
- m_Animator.SetBool("Jumping", false);
- Debug.Log("I am Jumping");
- }
- }
- void Flip()
- {
- facingRight = !facingRight;
- Vector3 Scaler = transform.localScale;
- Scaler.x *= -1;
- transform.localScale = Scaler;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement