Rigidbody2D does not contain totalForce?

Hello,

I am trying to program a Rigidbody-based 2D platformer. I want to have a method that can apply force to an object, but only enough so that they reach a certain maximum velocity. To to this, I want to use the TotalForce value to see what the current applied forces are, so the method cannot accelerate the object past its max velocity. Here is some test code I made to try and print totalForce:

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

public class ImpulseTester : MonoBehaviour
{
    Rigidbody2D rb;

    float direction = 1.0f;

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

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown("space"))
        {
            direction *= -1.0f;
            rb.AddForce(new Vector2(20.0f * direction, 0.0f), ForceMode2D.Impulse);
            Debug.Log(rb.totalForce.x); //This is where I try to get totalForce
        }
    }
}

Unfortunately, when I try to grab totalForce from a Rigidbody2D, I get this error message:

‘Rigidbody2D’ does not contain a definition for ‘totalForce’ and has no accessible extension method ‘totalForce’ accepting a first argument of type ‘Rigidbody2D’ could be found (are you missing a using directive or an assembly reference?)

Anyone know the problem? I think it might be from the “using” directives at top.

And you’re using Unity 2022.2 onwards correct? It’s nothing to do with using; Rigidbody2D is clearly in the UnityEngine namespace and you haven’t mentioned a problem with that.

I’m actually using Unity 2021, thanks for asking! Does that value exist there under a different name? Or do I need to update to Unity 2022?

It was a new feature in 2022.2

Alright, I just updated to Unity 2022, should work now. Thank you for the help!