[SOLVED] Collision with enemies

A

AndreeaKage

Guest
Hi :) I'm trying to make my first game , so I'm not doing that well with coding. Can you please help me with something?
My game is a runner type of game . My player object can only jump and enemies ( car objects) come towards the player. When these two objects collide it's game over ,but I want to make the player to able to sit on top of the car and lose only when he hits the front of the car . I tried to do something with the collision mask , but when I change something the cars dissapear when they are out of the room.
Can you please give me some advice? ( I will attach two images , maybe it will be helpful.)
test1.png
test2.png
 

NightFrost

Member
You need to separate the collision checks where they are having distinct effects. Basically:
  • When car moves towards left and collides with player, game over.
  • When player is falling downwards (due to gravity) and collides with either the road or a car, stop falling.
  • Gravity check: when player is not standing on top of the road or a car, make them fall downwards.
 
S

Silver_Mantis

Guest
Hello AndreeaKage!
This problem should not be that hard to figure out.

So let's get started!

So within your Player Object (obj_Cat) you need to add a Collision Event.
Drag a code piece into the event and write the following:

Code:
var above_car = y < other.y+vspd;
var falling = vspd > 0;
These are variables you will need in order for the collision to work.
(vspd is actually the variable for the Y axis, or obj_Cat.y, just so you know)

Then underneath that, write the code that will stop you from being hurt from the top of the car.

Code:
if (above_car && falling)
{
//Bounce Off Of Car
// This code will only work if you have some kind of active gravity code running to tell the cat to fall back down.
    vspd = -10;
}
else
{
//End game here, or restart the room
game_end();
}
This code should work for you! :D
 
A

AndreeaKage

Guest
Thank you so much Silver_mantis !!! I will try out this code and I will tell you how it worked ;) And thank you NightFrost for the advice :D I'll try my best to make it work
 
A

AndreeaKage

Guest
Hello AndreeaKage!
This problem should not be that hard to figure out.

So let's get started!

So within your Player Object (obj_Cat) you need to add a Collision Event.
Drag a code piece into the event and write the following:

Code:
var above_car = y < other.y+vspd;
var falling = vspd > 0;
These are variables you will need in order for the collision to work.
(vspd is actually the variable for the Y axis, or obj_Cat.y, just so you know)

Then underneath that, write the code that will stop you from being hurt from the top of the car.

Code:
if (above_car && falling)
{
//Bounce Off Of Car
// This code will only work if you have some kind of active gravity code running to tell the cat to fall back down.
    vspd = -10;
}
else
{
//End game here, or restart the room
game_end();
}
This code should work for you! :D
It worked !! :D Thank you again for the code and for explaining it ! One more question I have if I may ask... I have 2 cars that come towards the player and when they are outside the room they wrap horizontal , but after some time the cars come almost at the same time , here are some images : masini test 2.png same time.png
What did I do wrong ? I don't know exactly how to make a spawner but I thought this would work... :( Please help me out
 
S

Silver_Mantis

Guest
It worked !! :D Thank you again for the code and for explaining it ! One more question I have if I may ask... I have 2 cars that come towards the player and when they are outside the room they wrap horizontal , but after some time the cars come almost at the same time , here are some images : View attachment 8327 View attachment 8328
What did I do wrong ? I don't know exactly how to make a spawner but I thought this would work... :( Please help me out
Oh yeah I have lots of spawners in my own games!
I'll teach you how to make a basic one.
(So I'm assuming you would like the cars to delete once they hit the area behind the cat correct?
It would make no sense for them to remain after that point or outside of the map)

First things first. Let's make a spawner.

Create an object called obj_Spawner.
Then once inside, create an Alarm. Default would be alarm[0].
Inside alarm[0], drag a code block and type the following:

Code:
///Timer for cars
I know this is very simple, but I'll explain why.
You see in order for the alarm to work, it needs at least one piece of code to be activated. Event if that code is blank.
It's good practice to name what the alarm will be doing though, so you can remember why you have the code there.
(Also if you didn't know, using ' /// ' (Three slashes instead of two) when commenting at the top of your code, will actually name that file!

Okay so now for the running code. Create a Step Event in obj_Spawner.
Within the Step Event write something like this:

Code:
///Spawn Cars
// This code reads, if NOT alarm[0], which means we are checking to see if it's off.
if (!alarm[0])
{
// This creates one car of your choice.
    instance_create(x, y, obj_Car);
// If you wanted to randomize the cars that come out, use "choose()" which allows for up to 16 random items to spawn.
// instance_create(x, y, choose (obj_Car, obj_Car2, obj_Car3));

// Now we need to set the alarm to continually spit out cars!
// Do this by setting the timer to room speed times the number of seconds you want the timer to last.
// Don't forget that timers go off at " -1 " so this means that " room_speed * 2 " is actually 3 seconds.
    alarm[0] = room_speed * 2;
}
Now all we need is to tell the cars to delete after a certain amount of time.
(Make sure you get rid of the code that makes them wrap horizontal)
So within your obj_Car, create another Alarm like we did in the spawner.

Then in the Step Event for the car, add the following code:
Code:
if (!alarm[0])
{
alarm[0] = room_speed * 5
}

if (alarm[0] == 0 )
{
instance_destroy();
}
Add the spawner to your room!
(Don't forget the cars spawn at the exact X and Y values of the spawner.)
You should be good to go with that concept! :cool:
 
Last edited by a moderator:
A

AndreeaKage

Guest
Oh yeah I have lots of spawners in my own games!
I'll teach you how to make a basic one.
(So I'm assuming you would like the cars to delete once they hit the area behind the cat correct?
It would make no sense for them to remain after that point or outside of the map)

First things first. Let's make a spawner.

Create an object called obj_Spawner.
Then once inside, create an Alarm. Default would be alarm[0].
Inside alarm[0], drag a code block and type the following:

Code:
///Timer for cars
I know this is very simple, but I'll explain why.
You see in order for the alarm to work, it needs at least one piece of code to be activated. Event if that code is blank.
It's good practice to name what the alarm will be doing though, so you can remember why you have the code there.
(Also if you didn't know, using ' /// ' (Three slashes instead of two) when commenting at the top of your code, will actually name that file!

Okay so now for the running code. Create a Step Event in obj_Spawner.
Within the Step Event write something like this:

Code:
///Spawn Cars
// This code reads, if NOT alarm[0], which means we are checking to see if it's off.
if (!alarm[0])
{
// This creates one car of your choice.
    instance_create(x, y, obj_Car);
// If you wanted to randomize the cars that come out, use "choose()" which allows for up to 16 random items to spawn.
// instance_create(x, y, choose (obj_Car, obj_Car2, obj_Car3));

// Now we need to set the alarm to continually spit out cars!
// Do this by setting the timer to room speed times the number of seconds you want the timer to last.
// Don't forget that timers go off at " -1 " so this means that " room_speed * 2 " is actually 3 seconds.
    alarm[0] = room_speed * 2;
}
Now all we need is to tell the cars to delete after a certain amount of time.
(Make sure you get rid of the code that makes them wrap horizontal)
So within your obj_Car, create another Alarm like we did in the spawner.

Then in the Step Event for the car, add the following code:
Code:
if (!alarm[0])
{
alarm[0] = room_speed * 5
}

if (alarm[0] == 0 )
{
instance_destroy();
}
Add the spawner to your room!
(Don't forget the cars spawn at the exact X and Y values of the spawner.)
You should be good to go with that concept! :cool:
Thank you !!! <3 Works great ! Some of the code in general seems weird to me , maybe because I'm not used to GML ,but some lines of the code are pretty familiar ( to me it looks like c++ language which we learn at school). Anyway thanks again for helping me ;)
 
S

Silver_Mantis

Guest
Thank you !!! <3 Works great ! Some of the code in general seems weird to me , maybe because I'm not used to GML ,but some lines of the code are pretty familiar ( to me it looks like c++ language which we learn at school). Anyway thanks again for helping me ;)
Im glad I could help. Just add [SOLVED] to your title! :)
 
Top