Home > Net >  How to make Unity gravity and AddForce accelerate
How to make Unity gravity and AddForce accelerate

Time:07-14

I'm making a clone of Hollow Knight, and my character is falling at a constant rate instead of accelerating. I tried changing the gravity scale and using Addforce instead of rigidbody gravity.

This is the code I tried for the gravity

public Rigidbody2D rb;
void FixedUpdate(){
    rb.AddForce(-transform.up*100f * Time.fixedDeltaTime, ForceMode2D.Impulse);
}

CodePudding user response:

I've done a lot of testing and it doesn't seem like what you say is true. In a new project, I simply created a rigidbody2D gameobject and added this script to it.

public void Start()
{
    StartCoroutine(PrintDistance());
}

IEnumerator PrintDistance()
{
    float p = 0;
    for (; ; )
    {
        p = transform.position.y;
        yield return new WaitForSeconds(1);
        print("Distance per second: "   (transform.position.y - p));
    }
}

As you can see from the console, every second, the distance traveled increases, so the speed is not constant but accelerated. How can you see that the falling speed is not constant? Try adding this script and tell me the results. I can speculate that some other part of your project is causing this problem, or you have changed some properties in the Physics panel in Project Settings, but that is not likely.

if you think my answer helped you, you can mark it as accepted. I would very much appreciate it :)

  • Related