• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

checking if a variable is either more(or less) than a positive and negative number

K

Kamon145

Guest
Hello everyone! I am currently working on a platformer and my player has a maximum run speed that i would like to keep them from going over, but other objects in the game (such as explosions, ect) should still be able to add speed, my current code as it stands is
Code:
spd[h] +=hinput*accel//add acceleration to speed either positive or negative depending on value returned
spd[h] = clamp(spd[h],-spd_max,spd_max)
x+=spd[h]
hinput returns either 1 or -1 depending on direction pressed,
this works for keeping the player from going over speed, but prevents additional speed from being added, i have tried something along the lines of adding a check at the beginning
Code:
if spd[h] != hinput*run_spd
spd[h] +=hinput*accel//add acceleration to speed either positive or negative depending on value returned
    spd[h] = clamp(spd[h],-spd_max,spd_max)
but this dosent work because i assume the exact value of spd_max is being skipped over, and I've tried using <= and >= and they both work, but only in one direction, is there a function to check if a number has gone past a value? like "if spd[h] is between -run_spd, and run_spd"?
 

FrostyCat

Redemption Seeker
The most basic form is this:
Code:
if (spd[h] >= -run_spd && spd[h] <= run_spd)
And because the range is in the form [-xxx, xxx] (i.e. symmetric around 0), this special form can also be used:
Code:
if (abs(spd[h]) <= run_spd)
 
Top