Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to walk with AddForce without having too much force upon start

Discussion in '2D' started by lordumbilical, May 11, 2023.

  1. lordumbilical

    lordumbilical

    Joined:
    May 24, 2020
    Posts:
    38
    My 2D player can walk and push objects via RB.Addforce. The problem is: in order to get the player to start walking quickly, a lot of force needs to applied each FixedUpdate, so that within a frame or two he is up to maximum speed. I limit the velocity to prevent further acceleration and apply a massive drag so he stops quickly when keys are released, behaving like a traditional character that is moved with transforms. However, this means my player can push massive objects too easily when starting to walk, because I am exaggerating forces to get him moving quickly. So, I need a way to move my player with a realistic 2d fashion( from stop to full speed near instantly), using forces, so that he can interact with physics objects, without this overpowering effect. Methods like RB.MovePosition or directly setting RB.velocity create the same problem; objects are just pushed forward no matter their mass. My only solution I can think of is to start using Collision detection and manually remove forces in this situation, but I'm hoping for a more elegant solution. I'm hoping I have missed something very obvious. Any suggestions appreciated. Thanks in advance.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,968
    for better game feel I definitely recommend to manually use .velocity.
    Then when you push an object, apply can offset/multiplier to the speed
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I definitely second this recommendation.

    And if you need the speed to change smoothly rather than all at once:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    If you do insist on doing it with forces you will need a PD filter controller.

    https://en.wikipedia.org/wiki/PID_controller

    You can almost certainly do without the
    I
    part and just do the
    PD
    part. :)
     
  4. lordumbilical

    lordumbilical

    Joined:
    May 24, 2020
    Posts:
    38
    Thanks for the great advice so far. My temporary solution, as bizarre as it sounds, is to set my player mass to almost zero, with a high drag. He accelerates instantly, and when he is bumped into by objects still resists as if he has normal mass due to the drag. It probably has pitfalls I havent bumped into yet, but it is doing the job of at-least appearing to simulate the physics I am looking for. It's a bandaid solution while I attend to other things, but I will definitely come back and try the PID controller...which I used to play with years ago when drones first came out. Interesting indeed.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Huh, that actually sounds kinda interesting. I hope it works well for you!

    When you say that it reminded me of this great discussion of a physics character controller:



    Check out the car one too:



    Brilliant stuff.
     
  6. lordumbilical

    lordumbilical

    Joined:
    May 24, 2020
    Posts:
    38
    Yep. The physics based character controller is pretty much exactly what I was looking for. Awesome , and a big thanks.
     
    Kurt-Dekker likes this.