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);
}
}
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
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).
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);
}
}