The player not jumping

1st Unity project. I (I imagine) changed something without intention, and my player no longer jumps… it prints “jump” before and after the Addforce command, and the tag and bool “isfeet” is correctly setup


. Thanks in advance for the help

using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IntroductionCSharp : MonoBehaviour
{
    public string pnjname;
    public float speed;
    public float powerJump = 2f;
    public PlayerFeet feet;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("hello");
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 currentVelocity = new Vector2(0, GetComponent<Rigidbody2D>().velocity.y);
        if (Input.GetKey(KeyCode.Q))
        {
            currentVelocity.x -= speed;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            currentVelocity.x += speed;
        }
        GetComponent<Rigidbody2D>().velocity = currentVelocity;

        //saut 
        if (Input.GetKeyDown(KeyCode.Space) && (feet.isGrounded))
        {
            Debug.Log("jump");
            GetComponent<Rigidbody2D>().AddForce(Vector2.up*powerJump);
            Debug.Log("la c'est bon j'ai jump");
        }
    }
}

What is the powerJump value as shown in the Inspector?

It looks like it should work, maybe the powerJump is 0 for some reason? Also try caching the rigidbody and not looking for it every time. I can also recommend to tidy up the project by having Environment as a parent and assigning the spikes and stuff as children.

2 Answers

2

Hello. You should change the flag from InputSystem to Input or InputManager as this way to talking input does not refer to InputSystem.

3 just isn’t very much force by default. So you’re jumping, but only a couple of cm.

AddForce has two modes, “Force”, and “Impulse”. The default one that you’re using, force, is meant for use when you apply the force every frame in FixedUpdate (think thruster for a spaceship, player running), which essentially means that it multiplies your force value with Time.fixedDeltaTime.

What you want is to use the Impulse mode, which adds the force as a, well, impulse. You do it like this:

GetComponent<Rigidbody2D>().AddForce(Vector2.up * powerJump, ForceMode2D.Impulse);

You could also just add directly to the velocity, by simply doing:

var rb = GetComponent<Rigidbody2D>();

var currentVelocity = new Vector2(0, rb.velocity.y);

if (Input.GetKey(KeyCode.Q))
    currentVelocity.x = speed;
else if (Input.GetKey(KeyCode.D))
    currentVelocity.x = speed;

if (Input.GetKeyDown(KeyCode.Space) && (feet.isGrounded))
    currentVelocity.y += powerJump;

rb.velocity = currentVelocity;

The difference between doing that and doing AddForce is that the mass of your Rigidbody will not be taken into account. If you’re not planning on changing the mass of your Rigidbody from the default of 1, there won’t be any difference between AddForce with ForceMode2D.Impulse and adding to the velocity.