So I’m building a running game. As of right now his functions are jumping and running. I’m having a bug problem after lands from a jump sometimes gets stuck and can’t jump again . Can someone help me fix this error
We can’t help you with little information. Please let us know what happen in your scene. At least share screenshot from the scene or piece of code whatever you using.
public float moveSpeed;
private float moveSpeedStore;
public float speedMultiplier;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
private bool stoppedJumping;
private bool canDoubleJump;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
public Transform groundCheck;
public float groundCheckRadius;
//private Collider2D myCollider;
private Animator myAnimator;
public GameManager theGameManager;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D>();
//myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
jumpTimeCounter = jumpTime;
moveSpeedStore = moveSpeed;
stoppedJumping = true;
}
// Update is called once per frame
void Update() {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if ((Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) && !stoppedJumping)
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;
}
if(!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
}
}
if (Input.GetKey (KeyCode.Space) || Input.GetMouseButton(0))
{
if(jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if (grounded)
{
jumpTimeCounter = jumpTime;
canDoubleJump = true;
}
myAnimator.SetFloat(“Speed”, myRigidbody.velocity.x);
myAnimator.SetBool(“Grounded”, grounded);
}