Search Unity

Velocity clamp in FixedUpdate not stopping the force from an impulse when it should.

Discussion in '2D' started by _Railgun, Sep 12, 2019.

  1. _Railgun

    _Railgun

    Joined:
    Aug 7, 2015
    Posts:
    2
    Hello,

    I've created a script where the character's 3 hit combo has impulse forces applied to add movement. However, I don't want these movements to cause the character to fall off of a ledge. To prevent this, I have a raycast in Update checking for ground in front of the player. If the raycast does not detect ground, an if statement in FixedUpdate is allowed to run which clamps the horizontal velocity to 0.

    The problem is, for some reason the impulse forces sometimes aren't getting stopped by the velocity clamp when they push the player over an edge. If the raycast returns false before the force is even added, the script works properly. My only guess is that the velocity clamp statement in FixedUpdate isn't checking fast enough to stop the impulse forces. Whether this is correct or not, I'm not really sure how to solve this and was hoping you guys could help.

    Also keep in mind I don't want to stop the impulse force outright if the engine detects that it would go over the edge, I want it to still push itself as far as it can but get stopped once it reaches the edge.

    Impulse forces are applied from a function that is called from animation event keyframes.

    Below are two gifs. The MAGENTA line on the front foot is what's looking for ground. In the first script my problem occurs. In the second gif you can see an example of when it DOES work as it should.

    The problem: https://i.imgur.com/z8UWHtx.gifv

    When it actually works: https://i.imgur.com/Nxzgr0U.gifv
     
    Last edited: Sep 14, 2019
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,922
    You say "impulse forces" so do you mean impulse or force? Impulse will immediately modify the velocity whereas a force will just be accumulated and used when the simulation runs. There's no current way to zero those.

    You also say you "have a raycast in Update". Why are you doing that in update if the physics is updated in fixed-update? The bodies/colliders won't move each frame.

    Beyond that, it's hard to say what's going on here without seeing something.
     
  3. _Railgun

    _Railgun

    Joined:
    Aug 7, 2015
    Posts:
    2
    Hello, I am adding an impulse to the character, rather than a regular force since it happens in a single function call. Also, I'm certainly no expert so I could very well be wrong but I looked for whether I should do raycasts in Update or FixedUpdate and every source I found said Update was better. This made sense to me since it seems the raycast should find out whether or not there is ground below me as fast as possible. I would be grateful if you could explain why this thought process is incorrect. In the meantime, I'll try moving it to FixedUpdate.

    I guess I should have included my scripts as well. Here are the two main scripts that handle movement:
    https://pastebin.com/m799SsgE
    https://pastebin.com/MJzbdrXu

    The first script titled Movement is where most of this happens Key points are at lines 68, 91, and 125.

    The second one is a pre-written script I downloaded and heavily edited for the character movement. Anything else that you don't see in the scripts is handled in various state machine scripts or event keyframes. The only things happening in one of those that affects this issue is the AttackPush function being called via keyframes and the LockXMovement bool being set true on animation enter and false on animation exit.

    Thanks for taking a look.
    EDIT: The raycast associated with my attack animation is GroundFalloffCast. Also, ignore the comment on line 91, I forgot to remove it. It's there for me because I keep forgetting where the statement is lol