I tried everything, but the player wont jump.
Can you help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PlayerControler : MonoBehaviour
{
private Rigidbody2D PlayerRigidbody2D;
public float speed = 10f;
public float JumpForce;
private float MoveInput;
private bool FacingRight = true;
private SpriteRenderer mySpriteRenderer;
private bool IsGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJump;
public int MaxJumps;
void Start()
{
extraJump = MaxJumps;
mySpriteRenderer = GetComponent<SpriteRenderer>();
PlayerRigidbody2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
MoveInput = Input.GetAxisRaw("Horizontal");
PlayerRigidbody2D.velocity = new Vector2(MoveInput * speed, transform.position.y);
if(MoveInput > 0 && FacingRight == false)
{
mySpriteRenderer.flipX = false;
FacingRight = true;
} else if(MoveInput < 0 && FacingRight == true)
{
mySpriteRenderer.flipX = true;
FacingRight = false;
}
}
void Update()
{
IsGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
if (IsGrounded == true)
{
extraJump = MaxJumps;
}
if(Input.GetKeyDown(KeyCode.UpArrow) && extraJump > 0)
{
PlayerRigidbody2D.velocity = Vector2.up * JumpForce;
extraJump--;
} else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJump == 0 && IsGrounded == true)
{
PlayerRigidbody2D.velocity = Vector2.up * JumpForce;
}
}
}