• 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!

Legacy GM Throw back player when colliding

T

tomocska602

Guest
Hello guys!
How can I make an object (in this case a spike) throw back my player with a specified force?
I'd like this to work like this:
When my player object hits a spike from any direction, apply a force contrary to the direction the player is hitting the spike. Also, I must make sure my player only loses 1 health.
Any tips how could I achieve this? I got to the point where my player hits the spike from the top and succesfully bounces back, but sometimes it takes away 2 or more health :/
 
D

Dave Martinez

Guest
I would do something like this:

Create:
Code:
hit=0
myhp=100

Step:
Code:
//Right Collision
if hit = 0 and collision_line(x,y,x+sprite_width+1,y,obj_spike,1,0)
{
    hit = 1
    if place_free(x-sprite_width,y)
    {
    x-=sprite_width-1
    }

myhp-=1
hit = 0

}
 
T

Trilient

Guest
I actually have a spike object, in my case the ones I have slide up out of the ground if it detects the player object. Here's what I use for the collision aspect:

Code:
if place_meeting(x, y, oPlayer) && !oPlayer.damaged {
        oPlayer.Health -= dmg;
      
        with (oPlayer){
            vsp = -jumpspeed * 0.8;
            damaged = true;
        }
    }
Don't worry about the damaged stuff, it's a state I set for the player after he is hurt, the main stuff is that the spikes, since they slide up, cause the player to bounce off them vertically. It can easily be modified the throw the player in other directions.
 
T

tomocska602

Guest
I actually have a spike object, in my case the ones I have slide up out of the ground if it detects the player object. Here's what I use for the collision aspect:

Code:
if place_meeting(x, y, oPlayer) && !oPlayer.damaged {
        oPlayer.Health -= dmg;
     
        with (oPlayer){
            vsp = -jumpspeed * 0.8;
            damaged = true;
        }
    }
Don't worry about the damaged stuff, it's a state I set for the player after he is hurt, the main stuff is that the spikes, since they slide up, cause the player to bounce off them vertically. It can easily be modified the throw the player in other directions.
Thanks you for your responses!
Trillient, I already tried this and its working but I'd like it to work if the player hits the spike feom left(on the ground, without moving)
 
So you said you have everything figured out except that sometimes it takes away more than one health?
I would suggest turning on an alarm after colliding with the object, and while the alarm is greater than 0 your player can't take damage from the damage part of the collision.

So if you have something like a collision in your player event with a spike object, I would make it:
Code:
if(other.alarm[0] <= 0){
  self.hitpoints -= 1;
  other.alarm[0] = room_speed; //so it takes one second cooldown before damage is applicable again
}
 
T

tomocska602

Guest
So you said you have everything figured out except that sometimes it takes away more than one health?
I would suggest turning on an alarm after colliding with the object, and while the alarm is greater than 0 your player can't take damage from the damage part of the collision.

So if you have something like a collision in your player event with a spike object, I would make it:
Code:
if(other.alarm[0] <= 0){
  self.hitpoints -= 1;
  other.alarm[0] = room_speed; //so it takes one second cooldown before damage is applicable again
}

For some reason I can only lose one life with each spike.(the same spike only applies the effect once)
The only problem is that the player doesn't 'jump' when it hits the spike from the side (not jumps on it)

code i have it in the spike step event:

Code:
if place_meeting(x, y, obj_player){ 
        with (obj_player){
            vsp = -jumpspeed * 0.8;
            
                if(other.alarm[0] <= 0)
                {
                  self.lives -= 1;
                  other.alarm[0] = room_speed; //so it takes one second cooldown before damage is applicable again
                }
            
        }
    }
 
For some reason I can only lose one life with each spike.(the same spike only applies the effect once)
The only problem is that the player doesn't 'jump' when it hits the spike from the side (not jumps on it)

code i have it in the spike step event:

Code:
if place_meeting(x, y, obj_player){
        with (obj_player){
            vsp = -jumpspeed * 0.8;
         
                if(other.alarm[0] <= 0)
                {
                  self.lives -= 1;
                  other.alarm[0] = room_speed; //so it takes one second cooldown before damage is applicable again
                }
         
        }
    }
Okay, so this is something you do not place in the step event. You would create a collision event in your player object for the spike object.
In a collision event, it already handles checking if the two objects are meeting. If the two objects are meeting, then the collision event is called. In a collision event, the variable other is the object that the player is colliding with.

So the code would look like:
Code:
vsp = -jumpspeed * 0.8;
if(other.alarm[0] <= 0) {
  self.lives -= 1;
  other.alarm[0] = room_speed; //so it takes one second cooldown before damage is applicable again
}
If this code still does not deliver the effect you need (the jump back movement), then I will need to know how you are handling movement.

Note: I do not know where the vsp and jumpspeed variables originate from. If they originate from the player, then the above code does not need to be modified. If they originate from the spike, then the change should be :
Code:
 other.vsp = -other.jumpspeed * 0.8
 
Last edited:
T

tomocska602

Guest
Okay, so this is something you do not place in the step event. You would create a collision event in your player object for the spike object.
In a collision event, it already handles checking if the two objects are meeting. If the two objects are meeting, then the collision event is called. In a collision event, the variable other is the object that the player is colliding with.

So the code would look like:
Code:
vsp = -jumpspeed * 0.8;
if(other.alarm[0] <= 0) {
  self.lives -= 1;
  other.alarm[0] = room_speed; //so it takes one second cooldown before damage is applicable again
}
If this code still does not deliver the effect you need (the jump back movement), then I will need to know how you are handling movement.

Note: I do not know where the vsp and jumpspeed variables originate from. If they originate from the player, then the above code does not need to be modified. If they originate from the spike, then the change should be :
Code:
 other.vsp = -other.jumpspeed * 0.8
Thank You!
With your help I got the live system working properly. I created a 'couldbehit' boolean and set it to true. When the player collides with the spike:
Code:
///damage

if (couldbehit == true)
{
    vsp = -jumpspeed * 0.8;
    self.lives -= 1;
    alarm[0] = room_speed;
    couldbehit = false;
    }
In the alarm[0] event:
Code:
couldbehit = true;
If i fall or jump on the spike my player loses 1 live and jumps with 0.8 jump speed as it should be.
However, if i walk into the spike while staying on the ground i lose one live but i go straight through it :/

Attached an image with my setup.
 

Attachments

Any ideas how could I solve this problem?
Seems like your player object is not using physics? From what you have described, it seems to me like your objects should be using physics.

You need to make sure your player object uses physics, and that the spike also uses physics.
Then you should modify the collision shape properties of both to conform to their appropriate sprite dimensions.
 
Top