Page 1 of 1

howTo "velocity max"

PostPosted: Mon Sep 05, 2011 4:21 am
by RippeR7420
Is there a way to to set the maximum velocity of x and/or y?
In my game when my character collides with the Top of an enemy,
I have it set to bounce him in the air.but some times it launches my
Character really high in the air... how can I limit it to a certain velocity?

My code now is just:


Enemy- collision- bottom side of player- reapeat no- script editor-
Code: Select all
player.yvelocity=player.yvelocity - 20;


Thanks to whom will help!

Re: howTo "velocity max"

PostPosted: Mon Sep 05, 2011 4:25 am
by skydereign
Well, you can use this code. It uses the functions min and max, which returns the minimum or maximum of the two variables you pass it. What this does is first limit the bottom, so it can't be less than -20, then takes that velocity and says it can't be higher than 20.
Code: Select all
xvelocity=min(max(xvelocity, -20), 20);
yvelocity=min(max(yvelocity, -20),20);

Re: howTo "velocity max"

PostPosted: Mon Sep 05, 2011 5:27 am
by RippeR7420
Where would I use this code at?

Under Player-> Draw Actor-> Script Editor?

or Enemy-> Collsion-> Bottom Side -> No -> Script Editor?

Thank you!!

Re: howTo "velocity max"

PostPosted: Mon Sep 05, 2011 5:33 am
by skydereign
Well it depends. I'd probably write a global function out of it, and use it whenever I change the actor's velocity, but essentially you can put it in the player's draw if you want to limit the player's velocity. Or, if you don't edit the player's velocity often, you can put it into the collision event.

Re: howTo "velocity max"

PostPosted: Mon Sep 05, 2011 10:22 pm
by RippeR7420
Cool :)

Thanks sky!

There's one issues with that code, the xvelocity effects my player when I jump.
Like there is wind to my back? Should I just put the code for xvelocity in the collision
With an enemy only?

Re: howTo "velocity max"

PostPosted: Mon Sep 05, 2011 11:47 pm
by skydereign
If you don't need the xvelocity part, don't add it. You can also change the limits by changing the -20 and the 20. Do you understand how the code works? If you really do understand, then you should know where to put it and how to work with it.