Basic Bounce-Back code

E

EnSea

Guest
hello!
Sorry if this sounds demanding but I can't seem to figure out how to write a basic few lines of code to make an enemy bounce back when either hit or bumped into, like you would see in games like The Binding Of Isaac!
I want the code to be basic enough that I can add in some extra little details to the bounce, like maybe it'll grow a little bit then resize back down. I'm also wanting it be something that, instead of just writing this code every time I want something to bounce back, I can just have this code in a script and put the script in a little piece of code to use it.
For reference:
This is an RPG game where the sprites can only move 4 directions, like TBOI.
Also, as a side note, I'm not very experienced at this AND I'm still in highschool.
Thank you for reading this!
hopefully someone is kind enough to help me, as I know most of this community is ^-^
 

samspade

Member
hello!
Sorry if this sounds demanding but I can't seem to figure out how to write a basic few lines of code to make an enemy bounce back when either hit or bumped into, like you would see in games like The Binding Of Isaac!
I want the code to be basic enough that I can add in some extra little details to the bounce, like maybe it'll grow a little bit then resize back down. I'm also wanting it be something that, instead of just writing this code every time I want something to bounce back, I can just have this code in a script and put the script in a little piece of code to use it.
For reference:
This is an RPG game where the sprites can only move 4 directions, like TBOI.
Also, as a side note, I'm not very experienced at this AND I'm still in highschool.
Thank you for reading this!
hopefully someone is kind enough to help me, as I know most of this community is ^-^
Bounce back can be done in a lot of ways but how almost always depends on the way your characters move. What does their movement code look like?
 

Disaczar

Member
Hello. Sorry, I'm bad at English.

how do you want the enemy to bounce back? The angle of the fall is equal to the angle of the rebound, relative to the perpendicular to the wall that it hit (as in pong) or a simple rebound, that is, so that the enemy is just thrown back (as in streets of rogue)?
 
E

EnSea

Guest
Bounce back can be done in a lot of ways but how almost always depends on the way your characters move. What does their movement code look like?
Basically, I'm making a lot of new variables to try and help me understand everything. so I've got:

GML:
//Get player input
keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) or keyboard_check(ord("D"));
keyUp = keyboard_check(vk_up) or keyboard_check(ord("W"));
keyDown = keyboard_check(vk_down) or keyboard_check(ord("S"));
keyLeftRel = keyboard_check_released(vk_left) or keyboard_check_released(ord("A"));
keyRightRel = keyboard_check_released(vk_right) or keyboard_check_released(ord("D"));
keyUpRel = keyboard_check_released(vk_up) or keyboard_check_released(ord("W"));
keyDownRel = keyboard_check_released(vk_down) or keyboard_check_released(ord("S"));
keyAttack = keyboard_check_pressed(vk_space);
keyActivate = keyboard_check_pressed(ord("E"));
keyItem = keyboard_check_pressed(vk_control);


inputDirection = point_direction(0,0,keyRight-keyLeft,keyDown-keyUp);
inputMagnitude = (keyRight - keyLeft != 0) or (keyDown - keyUp != 0);

//Movement
hSpeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);

x += hSpeed;
y += vSpeed;

//Update sprite index
if keyRight {
    sprite_index = sprite_characterRight;
    image_speed = 1;}
if keyLeft {
    sprite_index = sprite_characterLeft;
    image_speed = 1;}
if keyDown {
    sprite_index = sprite_characterDown;
    image_speed = 1;}
if keyUp {
    sprite_index = sprite_characterUp;
    image_speed = 1;}

if keyRightRel {
    sprite_index = sprite_characterRightS;
    image_speed = 0;}
if keyLeftRel {
    sprite_index = sprite_characterLeftS;
    image_speed = 0;}
if keyDownRel {
    sprite_index = sprite_characterDownS;
    image_speed = 0;}
if keyUpRel {
    sprite_index = sprite_characterUpS;
    image_speed = 0;}

if(keyboard_check(vk_shift)) {
    speedWalk = 4.0;}
if(keyboard_check_released(vk_shift)) {
    speedWalk = 2.0;}
 
E

EnSea

Guest
Hello. Sorry, I'm bad at English.

how do you want the enemy to bounce back? The angle of the fall is equal to the angle of the rebound, relative to the perpendicular to the wall that it hit (as in pong) or a simple rebound, that is, so that the enemy is just thrown back (as in streets of rogue)?
sorry, I don't understand.
 

samspade

Member
This looks like player code not enemy code, but assuming that you want to apply a knockback effect to the player or the enemy code is similar it is pretty straight forward, simply apply force in the direction that you want using very similar code to your movement. This code might get you part of the way.

GML:
//where collision is determined however you're determining it
if (collision) {
    hKnockback = lengthdir_x(knockback_force, inputDirection);
    vKnocback = lengthdir_y(knockback_force, inputDirection);}

//in player step event
x += hSpeed + hKnockback;
y += vSpeed + vKnockback;

//reduce knockback by some increment to 0, remember knockback can be positive or negative
hKnockback = Approach(hKnockback, 0, vlaue);
vKnockback = Approach(vKnockback, 0, value);
The approach script can be found here: https://pastebin.com/7gzJTLKj

This code isn't the most efficient and collision won't effect it (but I didn't see any code for collisions in what you posted so perhaps that doesn't matter here).
 
Top