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.