#Why won't my 2d Square jump?

1 messages · Page 1 of 1 (latest)

raw creek
#

I have made this script ```using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMoveSc : MonoBehaviour
{

public float moveSpeed;
public float jumpForce;

public Rigidbody2D rb;

private Vector2 movement;

private void Update()
{

    float horizontal = Input.GetAxisRaw("Horizontal");
   // float vertical = Input.GetAxisRaw("Vertical");

    movement = new Vector2(horizontal, 0);

    if(Input.GetKeyDown(KeyCode.Space) && rb.velocity.y == 0)
    {
        Debug.Log("Should JUmp");
        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    }
}

private void FixedUpdate()
{
    rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime);
}

}

#

Why won't my 2d Square jump?

turbid epoch
# raw creek I have made this script ```using System.Collections; using System.Collections.Ge...

Debug.Log your rb.velocity.y or watch it in your inspector when you play
it is unlikely that this will ever be true
rb.velocity.y == 0
if gravity is applied, it keeps getting applied even if you stand on a floor, so the y would change, but the physics engine will depenetrate your object from the ground collider so it looks like it doesn't move

you should also add logic for a ground check, velocity check only is pretty fragile
but my code (which would also check if I stand on a ground) would usually be something like this
if(Input.GetKeyDown(KeyCode.Space) && rb.velocity.y <= 0.1f)
so when the velocity is around 0 or lower, I can jump again, maybe you just put <= 0f instead too

hearty compass