- Home /
This question was
closed Dec 21, 2020 at 03:44 PM by
InfernoGamingYT for the following reason:
Problem is not reproducible or outdated
Question by
InfernoGamingYT · Dec 20, 2020 at 05:42 AM ·
shooter2d platformer
character able to jump non-stop
My character can jump as much as they want. i dont want that, i only want a double jump. here is my code.
public float speed;
public float jumpForce;
public float moveInput;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public int extraJump;
public int ejV;
private bool isGrounded;
private bool facingRight = true;
private Rigidbody2D rb;
void Start()
{
extraJump = ejV;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (isGrounded == true)
{
extraJump = ejV;
}
if (Input.GetKey(KeyCode.Space) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump--;
}
else if (Input.GetKey(KeyCode.Space) && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void FixedUpdate()
{
isGrounded = 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();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
Comment
Answer by Hawaii_Dev · Dec 20, 2020 at 11:55 AM
Have you checked so "checkRadius" is not to big and that "whatIsGround" has the right layermasks. Other than that add the line "isGrounded = false" in the if statement where you jump.
sooo, im kinda a noob at coding(just started back up recently), ik a little bit, but not much XD. no matter how big or small the checkradius is, it doesnt change the jumping. And i have no clue if i put the isGrounded = false in the right place(its in the if statement in jumping tho). and the ground is on the right layermask.