Trying To Give Throw able Object Knockback

S

SoftwenOP

Guest
I'm currently messing around with a project in GMS 1.4. I've successfully implemented a mechanic which allows the player to pick up and throw objects. However, I've experienced difficulties getting the object to have proper knockback, or a bounce feature when colliding with an object while being thrown. I've tried various methods to no avail. If anything does happen, the object either goes into the opposite direction, but doesn't stop, or stops, but goes in the incorrect direction. This is the movement code of the object, held within the step event:

///Follow Player
if Stick = true {
x = obj_player.x;
y = obj_player.y;
}

//Be Thrown
if Dir1 = true {
move_towards_point(x, y+Spd, Spd);
}
if Dir2 = true {
move_towards_point(x, y-Spd, Spd);
}
if Dir3 = true {
move_towards_point(x+Spd, y, Spd);
}
if Dir4 = true {
move_towards_point(x-Spd, y, Spd);
}

This is the code which allows the player to throw the object:

///Throw
if place_meeting(x, y, obj_rock) {
var ID;
ID = instance_nearest(x, y, obj_rock);
if Facing = 1 && ID.Stick = true {
ID.Dir1 = true;
ID.Spd = 6;
ID.Stick = false;
}
if Facing = 2 && ID.Stick = true {
ID.Dir2 = true;
ID.Spd = 6;
ID.Stick = false;
}
if Facing = 3 && ID.Stick = true{
ID.Dir3 = true;
ID.Spd = 6;
ID.Stick = false;
}
if Facing = 4 && ID.Stick = true {
ID.Dir4 = true;
ID.Spd = 6;
ID.Stick = false;
}
}

The main method I tried was setting Spd to equal -6 upon collision with a static object, then while Spd is below zero, use the lerp function to try and slow it down until Spd is equal to zero. I'm open to any suggestions, even if it involves changing this code.
 
B

Bayesian

Guest
If I understand this correctly you would do this to make it bounce

Code:
Dir1 = !Dir1
Dir2 = !Dir2
Dir3 = !Dir3
Dir4 = !Dir4
to make it slow down you could do spd *= 0.5
 
S

SoftwenOP

Guest
If I understand this correctly you would do this to make it bounce

Code:
Dir1 = !Dir1
Dir2 = !Dir2
Dir3 = !Dir3
Dir4 = !Dir4
to make it slow down you could do spd *= 0.5
This has gotten me closer than before. Though the issue is that it will really only bounce if thrown to the right, and the object will continue to go into the opposite direction if picked up and thrown again. Maybe a state machine would be best?
 
Top