- Home /
Can Someone Please help me my 2D jump is broken i can infinite jump i tried to fix it now i can't jump!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour {
public float speed;
public float jump;
private float movement;
private Rigidbody2D rb;
private bool isGrounded;
private bool canJump;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
CheckSurroundings();
movement = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(movement * speed, rb.velocity.y);
if (Input.GetButtonDown("Jump"))
{
if (canJump)
{
rb.AddForce(new Vector2(rb.velocity.x, jump));
}
}
}
private void FixedUpdate()
{
CheckSurroundings();
}
private void CheckSurroundings()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
"canJump" is not set to true or false anywhere, did you mean to use "isGrounded" instead?
Answer by b4guw1x · Jul 20, 2020 at 01:58 PM
Hİ, i have got another algorithm for jump so i think you can try it
private Rigidbody2D playerRB;
public float jumpSpeed;
void Start()
{
playerRB = GetComponent<Rigidbody2D>();
footCollider = GetComponent<BoxCollider2D>();
}
void FixedUpdate(){
if (Input.GetKey(KeyCode.Space))
{
Jump();
}
}
void Jump()
{
//multi jump skill disabled.
if (!footCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) return;
playerRB.AddForce(new Vector2(0f, jumpSpeed));
PlayerAnimator.SetInteger("State", 3);
}
So i think you can try it
Answer by jmhoubre · Jul 20, 2020 at 09:39 PM
Hello,
it seems to me that canJump is never true in your code. Edit line 7:
private bool canJump = true;
Edit lines 30 to 35:
{
if (canJump) {
float timeBetweenTwoJumps = 2f;
canJump = false;
rb.AddForce (new Vector2 (rb.velocity.x, jump));
while (timeBetweenTwoJumps> 0) {
timeBetweenTwoJumps -= Time.delta.Time;
}
canJump = true;
}
Good luck (and sorry for any mistakes in English).
One thing does this code all have to be in the code i wrote or just the first one can you send me the code with $$anonymous$$e on how it should look thanks!
would i get rid of this if (Input.GetButtonDown("Jump")) { if (canJump) { rb.AddForce(new Vector2(rb.velocity.x, jump)); } } } private void FixedUpdate() { CheckSurroundings(); } private void CheckSurroundings() { isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround); } private void OnDrawGizmos() { Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
never $$anonymous$$d i tried your code and now he flies away into the sky how can i fix that
i lowered the jump strength to 15 and now i don't fly away but i can't do anything the cube just sits there like it has only just a rigid body...
This code is not correct, don't use a while loop in update like this. Try this instead
private float timeBetweenJumps = 0;
void Update () {
CheckSurroundings ();
movement = Input.GetAxis ("Horizontal");
rb.velocity = new Vector2 (movement * speed, rb.velocity.y);
//Add a short cooldown between jumps
if (timeBetweenJumps > 0) {
timeBetweenJumps -= Time.deltaTime;
}
//If we have waited for the cooldown and we are on the ground, then we can jump again
if ((timeBetweenJumps <= 0) && isGrounded) canJump = true;
else canJump = false; //If we are not grounded, we cannot jump
if (Input.GetButtonDown ("Jump") && canJump) {
canJump = false;
timeBetweenJumps = 0.2f;
rb.AddForce (new Vector2 (rb.velocity.x, jump));
}
}
Answer by BobMen645 · Jul 24, 2020 at 03:16 PM
There's no errors and I still can't jump!! here's my code!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private bool canJump = true;
public float speed;
public float jump;
private float timeBetweenJumps = 0;
private float movement;
private Rigidbody2D rb;
private bool isGrounded;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
CheckSurroundings();
movement = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(movement * speed, rb.velocity.y);
//Add a short cooldown between jumps
if (timeBetweenJumps > 0)
{
timeBetweenJumps -= Time.deltaTime;
}
//If we have waited for the cooldown and we are on the ground, then we can jump again
if ((timeBetweenJumps <= 0) && isGrounded) canJump = true;
else canJump = false; //If we are not grounded, we cannot jump
if (Input.GetButtonDown("Jump") && canJump)
{
canJump = false;
timeBetweenJumps = 0.2f;
rb.AddForce(new Vector2(rb.velocity.x, jump));
}
}
private void FixedUpdate()
{
CheckSurroundings();
}
private void CheckSurroundings()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
$$anonymous$$ake sure isGrounded is being set to true when you are on the ground. You can set it to public to check
I use raycasting to detect if I'm on the floor or not in my 3D game do you suggest me to stick with that?
Raycasting is fine depending on your game. OverlapSphere is more expensive but it checks an entire area under the character instead of just a point.
so I should change this line private bool isGrounded; to this public bool isGrounded = true; ??
I think that would work, what I do is that I use raycasting to detect if im touching the floor or not, I think that works best, but still it has alot of issues, try not using alot of raycasting since it can cause performance drops.
Your answer
Follow this Question
Related Questions
HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers
The player randomly freezes in place while other objects move ingame 1 Answer
Having trouble moving object in opposite direction of player. 1 Answer
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
Move game object in opposite direction of player in x & y axis? (2D Game) 0 Answers