Cant double jump with grounded code,Being grounded and Double Jumping
I have this code where it works great for me so far, except the jumping, no matter how I try to do it I can't seem to be able to double jump I've gotten to the point where I can double jump but it is an infinite double jump because I don't know how to set the JumpCounter back to zero because if I put it in the second jumping line of code the counter never goes up or it adds up together if I change the value to anything. ,I am trying to make it so that my player double jumps and have gotten to the point where if he jumps the value changes to 1 and he can double jump, if the value is not at 1 he will not double jump, my problem is resetting the value to 0 so that the player cant double jump anymore. If I type anything that has to do with JumpCount in the second line of jumping they both add up together or if I set JumpCount =0 it just wont move up.
public class Player_Controller : MonoBehaviour { Animator animator; Rigidbody2D rb2d; SpriteRenderer spriteRenderer;
bool isGrounded;
[SerializeField]
Transform groundCheck;
[SerializeField]
private float runSpeed = 2;
[SerializeField]
private float jumpSpeed = 3;
[SerializeField]
private int JumpCount = 1;
//use this for initialization
private void Start()
{
animator = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
if(Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if(Input.GetKey("d") || Input.GetKey("right"))
{
rb2d.velocity = new Vector2(runSpeed, rb2d.velocity.y);
if(isGrounded)
animator.Play("Player_run");
spriteRenderer.flipX = false;
}
else if(Input.GetKey("a") || Input.GetKey("left"))
{
rb2d.velocity = new Vector2(-runSpeed, rb2d.velocity.y);
if (isGrounded)
animator.Play("Player_run");
spriteRenderer.flipX = true;
}
else
{
if (isGrounded)
animator.Play("Player_idle");
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
}
if (isGrounded)
{
JumpCount = 0;
}
if(Input.GetKey("space") && isGrounded)
{
JumpCount = 1;
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed);
animator.Play("Player_jump");
}
if (Input.GetKey("space") && !isGrounded && JumpCount == 1)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed);
JumpCount = 2;
}
}
}