2

I'm trying to use rb.velocity in unity to make my 2D character jump. I have used Debug.Log() to check if unity registers the input, and it does. But it does not affect my character in game when pressing the button. Here's the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 7f;

    public Rigidbody2D rb;

    Vector2 movement;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
    }

    void FixedUpdate() 
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Space was pressed");
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
}

I have tried replacing the values with all sorts of things in the Vector2, but to no avail.

  • 1
    Could you share how your game object's rigidbody is set ? Also just for testing, set your jumpforce to a very large number to see if your character flies off the screen. Other thing is your `rb.MovePosition` line is also modifying your character's velocity.y and you don't want that. – Çağatay IŞIK Jan 12 '23 at 18:03

2 Answers2

2

If your rigidbody is using gravity then in this case a small vertical velocity of 7f would be negated by gravity very quickly (less than a second). You can check this in the physics settings of your project or with Rigidbody.useGravity. If you intend to use gravity then it is recommended to use Rigidbody.AddForce to add an upward force to the object and let gravity bring it back down - example SO answer here.

If you want to turn off gravity and not use AddForce then it would be best to either use Rigidbody.MovePosition or set Rigidbody.velocity, but not both.

For example:

void Update() {
    movement = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, movement.y);
}

void FixedUpdate() {
    if (Input.GetKeyDown(KeyCode.Space)) {
        movement.y = jumpForce;
    }
    rb.velocity = movement;
}

But in that case you would have to manually figure out when the jump is complete and set a downwards velocity to bring it back to the ground yourself. If you want the easiest and most natural solution, then leveraging Unity's built-in physics with gravity and forces would be best.

  • Could you restate that first sentence, because as it stands, it sounds completely wrong. A velocity is instantly overwritten by gravity? – Milan Egon Votrubec Jan 12 '23 at 22:41
  • @MilanEgonVotrubec you're right, it wouldn't be _instantly_ overwritten. But in this case if the upward velocity is +7 m/s and acceleration is -9.81 m/s^2 (gravity), then the velocity would be canceled out in less than a second. I'll revise to make that more clear. – mitchellgarrett Jan 12 '23 at 23:09
  • I have tried doing what you said, and implementing Rigidbody.AddForce. It still does not work though. I tried implementing the code in a new project to see if it was the project that was the problem, but it still doesn't work. – TheCookieLegend Jan 14 '23 at 19:55
  • Hmm AddForce is very common way to implement jumping and there's plenty of tutorials out there. Maybe something like [this](https://gamedevbeginner.com/how-to-jump-in-unity-with-or-without-physics/) could help, sorry I can't be more help but there might be something going on that's external to your code snippet. – mitchellgarrett Jan 18 '23 at 18:40
0

Honestly, your code looks fine. I would move the Input.GetKeyDown check into Update instead of FixedUpdate. But other than that it looks good.

Instead, make sure your jumpForce is not 0. Just because you set the initial value in your script

public float jumpForce = 7f;

Doesn't mean that it is 7f. Check the script in the inspector and make sure it's not 0. Setting values on scripts like you did only sets the initial value. If you created the jump force variable before assigning 7 to it, it will still be 0. You have to set the variables in the inspector.

And if that doesn't work use:

rb.AddForce(jumpForce * Vector3.up, ForceMode.Impulse);

Instead of trying to manually set the velocity.

CorrieJanse
  • 2,374
  • 1
  • 6
  • 23