Home > front end >  Cannot get rb.velocity to work when making a jumping script in unity 2D
Cannot get rb.velocity to work when making a jumping script in unity 2D

Time:01-13

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related