playMaker

Author Topic: Best way to Limit Rigidbody 2D velocity?  (Read 3605 times)

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Best way to Limit Rigidbody 2D velocity?
« on: June 22, 2018, 11:52:09 PM »
Tying to limit how fast my character can go in the X direction. Currently I have a Float Compare Range set to Fixed Update which then flows into a Set Velocity 2D state when the velocity exceeds the max, where it is then set to the max. The issue is that I get a looping warning. If I were to use a Next Frame event, I'm pretty sure that would destroy the whole point of using Fixed Update.

What should I do?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #1 on: June 23, 2018, 09:53:26 AM »
Hi.
have you tried a 'float clamp' ?

verybinary

  • Junior Playmaker
  • **
  • Posts: 81
    • The Museum of Digital
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #2 on: June 24, 2018, 03:54:20 PM »
When I get a 1000 loop, I usually find something else for the fsm to do to break the loop, or put in a wait action somewhere for 0.01. That seems to slow it down somewhere lower than 1000 no matter what's in the loop.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #3 on: June 24, 2018, 07:52:09 PM »
Hi.
@verybinary

That is actually a bad habit. (and believe me, i did that also before)
usually this can be prevented by doing everything in a single state or use 2 or more fsm's

and if you really need to do this then you can also use a 'next frame event' instead of a wait, which would be more consistent than a wait :)

verybinary

  • Junior Playmaker
  • **
  • Posts: 81
    • The Museum of Digital
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #4 on: June 26, 2018, 11:03:36 AM »
@djaydino thanks for the heads up. I hadn't even considered its cost, and very likely nothing has been released with it yet, as its only a placeholder until I find other things for the loops to do. but its possible some have been forgotten and made it to builds, so ill look into switching up the workflow for the accidentals.

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #5 on: July 22, 2018, 05:02:17 PM »
Hi.
have you tried a 'float clamp' ?

I downloaded the Float Clamp Advanced option from the Ecosystem. Using Get and Set Velocity 2D causes a very strange floaty effect. Not sure why that's happening. I decided to try using Get and Set Property on the Rigidbody 2D's Y velocity. If Float Clamp is set to Fixed Update, it just doesn't work at all. If it's set to Update it works.

The issue is that now the actions that limit the player's velocity occur in Update. Shouldn't physics based actions happen in Fixed Update? To be honest, working with specific Update types in Playermaker has been pretty difficult. Why don't all actions by default have an Update Type option?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #6 on: July 23, 2018, 04:33:05 AM »
Hi.
Float clamp is not a physics based action.

Can you show your setup (fsms/states/actions images or video)
Maybe a different setup can help.
How is the velocity set/added to the character.

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #7 on: July 23, 2018, 07:46:27 AM »
Hi.
Float clamp is not a physics based action.

Can you show your setup (fsms/states/actions images or video)
Maybe a different setup can help.
How is the velocity set/added to the character.

Ya sure. Currently it's just these 3 actions and a single state. Just trying to get a "terminal velocity" effect.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #8 on: July 23, 2018, 08:55:05 AM »
Hi.
How is the velocity set/added to the character.  (how does the character move)

i think the movement is interfering with the clam fsm you made.

Plancksize

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 75
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #9 on: July 23, 2018, 09:13:27 AM »
I'm a complete noob on all this but there's this action "SetVelocityConstantSpeed2d".
https://github.com/PlayMakerEcosystem/PlayMakerCustomActions_U5/blob/master/Assets/PlayMaker%20Custom%20Actions/Physics/SetVelocityConstantSpeed2d.cs

Could you possibly call it with the correct value at the moment that the player/object reaches it's terminal velocity?

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #10 on: July 23, 2018, 10:33:50 AM »
Hi.
How is the velocity set/added to the character.  (how does the character move)

i think the movement is interfering with the clam fsm you made.

The only other FSM that sets velocity every frame only deals with the horizontal (X is set to none and not 0). Just in case I didn't make this clear, the FSM I posted does actually work in clamping the velocity, it's just I'm pretty sure it's doing it in Update, and not FixedUpdate. Shouldn't physics related code by done in FixedUpdate?

Plancksize

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 75
Re: Best way to Limit Rigidbody 2D velocity?
« Reply #11 on: July 23, 2018, 12:10:49 PM »
Hi.
How is the velocity set/added to the character.  (how does the character move)

i think the movement is interfering with the clam fsm you made.

The only other FSM that sets velocity every frame only deals with the horizontal (X is set to none and not 0). Just in case I didn't make this clear, the FSM I posted does actually work in clamping the velocity, it's just I'm pretty sure it's doing it in Update, and not FixedUpdate. Shouldn't physics related code by done in FixedUpdate?

Just for curiosity, why are you using a "set property" in there instead of a "Set Velocity 2d" action?
PS: just checked that action code and indeed the "every frame" option is set as a FixedUpdate.