Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Approach(a, b, amount)
- // Moves "a" towards "b" by "amount" and returns the result
- // Nice bcause it will not overshoot "b", and works in both directions
- // Examples:
- // speed = Approach(speed, max_speed, acceleration);
- // hp = Approach(hp, 0, damage_amount);
- // hp = Approach(hp, max_hp, heal_amount);
- // x = Approach(x, target_x, move_speed);
- // y = Approach(y, target_y, move_speed);
- if (argument0 < argument1)
- {
- argument0 += argument2;
- if (argument0 > argument1)
- return argument1;
- }
- else
- {
- argument0 -= argument2;
- if (argument0 < argument1)
- return argument1;
- }
- return argument0;
Advertisement
Comments
-
- doesnt even work
-
- it says it undefined value when trying to run the code
-
Comment was deleted
-
- here is an updated code for newer GMS2 versions like (v2024.06.2.162)
- function Approach(_value,_value_to,_amount)
- {
- if (_value < _value_to)
- {
- _value += _amount;
- if (_value > _value_to)
- return _value_to;
- }
- else
- {
- _value -= _amount;
- if (_value < _value_to)
- return _value_to;
- }
- return _value;
- }
- Keep in mind you can't write the function and expect it to alter a value on its own. You have to write the whole instruction, like this:
- y = Approach(y, target_y, move_speed);
- and not
- Approach(y, target_y, move_speed);
Add Comment
Please, Sign In to add comment
Advertisement