Question by
LyraxH · Sep 29, 2020 at 10:58 AM ·
2d-platformerphysics2d
i can jump an infinite amount of times, how do i make it so i can only jump once,I can jump infinitly, even though i only want it once
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float movementSpeed; public Rigidbody2D rb;
public float jumpForce = 15f;
public Transform feet;
public LayerMask groundLayers;
float mx;
private void Update()
{
mx = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded())
{
Jump();
}
}
private void FixedUpdate()
{
Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
rb.velocity = movement;
}
void Jump()
{
Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
rb.velocity = movement;
}
public bool isGrounded()
{
Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
if(groundCheck != null)
{
return true;
}
return false;
}
0
} , using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float movementSpeed; public Rigidbody2D rb;
public float jumpForce = 15f;
public Transform feet;
public LayerMask groundLayers;
float mx;
private void Update()
{
mx = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded())
{
Jump();
}
}
private void FixedUpdate()
{
Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
rb.velocity = movement;
}
void Jump()
{
Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
rb.velocity = movement;
}
public bool isGrounded()
{
Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
if(groundCheck != null)
{
return true;
}
return false;
}
0
}
Comment
Your answer
Follow this Question
Related Questions
Need help making a ledge climber. The problem, transform.position always returns to vector( 0, 0) 0 Answers
Grapling gun help! Why doesn't my player move towards? Instead of getting stuck in place 0 Answers
How would I make a 2D gun knockback? 1 Answer
I want my player to walk through walls 1 Answer
FixedUpdate or Update for collision detection and climbing slopes? 1 Answer