rigidbody2d.AddForce not working

hi everyone, I’m doing some simple 2d tests.

i made a 2d player with rigidbody 2d.
I’m trying to jump by applying a force, but it doesn’t seem to work.
Where am I doing wrong?
I am attaching the rigidbody screen maybe I have some settings wrong, I tried to change the mass and gravity scale, but nothing.
I also attach the code, I put a log but the y position is always 0.0

if(Input.GetButtonDown("Jump")){
            Debug.Log("Jump");
            rigidbody2d.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
            Debug.Log(move);
        }

thank you in advance

Keep jacking that 10 up… it’s probably not even strong enough to (barely) overcome gravity for one frame.

Try 100
try 1000
try 10000

neither with 10000 work it

a little update, if i comment this line

void FixedUpdate()
    {
       
        Vector2 position = rigidbody2d.position;
        Debug.Log(position);
       
        position.x = position.x + 3.0f * horizontal * Time.deltaTime;
        /*
        rigidbody2d.MovePosition(position);
        */
    }

the addForce working correctly

Please show all your script, not pieces of it.

So for starters, you should NEVER modify the Transform when using 2D physics. The Rigidbody2D is in charge of the Transform and writes to it so you shouldn’t. What you’re doing here is even worse though; you want forces to work but you don’t understand how they work (they modify velocity which updates position) because you are also then asking the Rigidbody2D to move to an explicit position via MovePosition!

How can it do both? Answer: It cannot.

If you want to manipulate immediate changes as well as using forces then modify the X component (not the Y component) of the Rigidbody2D.velocity and let it update the position. You can then add impulses to jump which only affect the Y component of the velocity.

MovePosition just makes it move to that explicit position.

Sounds like you need to use Unity Learn and follow some of the basic 2D Physics tutorials on movement. Also there are lots on YouTube etc that should help.

1 Like

MovePosition forces it to a position, so of course velocity and forces are irrelevant and don’t work.

It’s like hitting the gas pedal in your car (AddForce) and tying your car to the ground so it goes nowhere.

thank you both. I changed the code a bit and it works fine, I removed the movePosition and did everything with addForce.
thanks again

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

public class Hero : MonoBehaviour
{  

    Rigidbody2D rigidbody2d;
    float horizontal;
    float vertical;
    public float moveSpeed;
    public float jumpForce;
    bool isJumping;

    Animator animator;
    Vector2 lookDirection = new Vector2(1,0);
    SpriteRenderer spriteRenderer;

    public float thrust = 5f;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();

        isJumping = false;

        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");

        Vector2 move = new Vector2(horizontal, 0);

        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {
            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();
        }

        if (lookDirection.x < 0)
        {
            spriteRenderer.flipX = true;
        }
        else
        {
            spriteRenderer.flipX = false;
        }

        animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Speed", move.magnitude);

       
    }
   
    void FixedUpdate()
    {
        if(horizontal > 0.1f || horizontal < -0.1f)
        {
            rigidbody2d.AddForce(new Vector2(horizontal * moveSpeed, 0f), ForceMode2D.Impulse);
        }
        if (!isJumping && vertical > 0.1f)
        {
            rigidbody2d.AddForce(new Vector2(0f, vertical * jumpForce), ForceMode2D.Impulse);
        }

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log(collision.gameObject.tag);
        if (collision.gameObject.tag == "Platform")
        {
            isJumping = false;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Platform")
        {
            isJumping = true;
        }
    }
}
1 Like