- Home /
Help I have no errors but I can't jump still!!
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);
}
}
@unity_ek98vnTRplGj8Q
Answer by smark12007 · Jul 24, 2020 at 06:14 PM
AddForce is an instant function if you knows f = ma. So, instead of adding force, directly set the velocity of the rigidbody makes your character jump easier.
Your answer
Follow this Question
Related Questions
I have a problem with coins 1 Answer
2D Platformer Bug 0 Answers
When I build my 2D game and set it to fullscreen, the resolution looks off 0 Answers
Projectile gets stuck at character's position (Unity2d) 0 Answers
Number rain background 2 Answers