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

Top-Down Enemy Collisions

M

Monster25

Guest
Hello! I'm trying to complete my game's enemy collisions. The player is intended to collide with walls, enemies and everything object I put under obj_solid as a child but it needs to work vice-versa with enemies as well, so if an enemy is moving towards the player, he would stop when he collides with the player. This totally worked fine but when I start and try to make collisions for my enemies when they hit the player it all goes wrong. The enemies rotate towards the player and they have 2 basic states, idle and chase. Everything works fine about them, I even fixed the image angle 💩💩💩💩ing up the collisions, everything is fine until they start moving. I'm not sure how I can make the code work for them. Here is the collision code from my state:

//Collision
clamp(movespeed,0,movespeedmax);
if (movespeed < movespeedmax)
movespeed = movespeed + accelerate_speed;
//Collision
if (place_meeting(x+2,y,obj_player))
speed = 0;
else
speed = movespeed;

if (place_meeting(x-2,y,obj_player))
speed = 0;
else
speed = movespeed;

if (place_meeting(x,y+2,obj_player))
speed = 0;
else
speed = movespeed;

if (place_meeting(x,y-2,obj_player))
speed = 0;
else
speed = movespeed;


As you can see I clamp the movespeed so it doesn't go over my max move speed then I use a small acceleration code so that the enemies don't move at once. Then for the collision code this is what I could come up with but they still get stuck. I've tried doing a similar collision code to my players' which is perfect but it doesn't work since I can't predict where they will move based on key strokes. They simply need to follow the player when they get in range, apart from the collision itself, everything else is perfect.
 
B

Blazing

Guest
Do the enemies and the player need to be checked as solid? Doing so means nothing can pass through them and often means things will get stuck inside of things if the collision goes wrong by even one pixel.

Also, look into the collision_circle and collision_rectangle functions for the enemy. It will clean up those repetitive place_meeting functions a bit.
 
M

Monster25

Guest
No they both have solid unchecked. I'm using the Spaulding collision for the player. Thanks for the tip about the collision functions!
 
B

Blazing

Guest
Another thought: you could also use
Code:
 if collision_circle(x+lengthdir_x(movespeed+2, direction), y+lengthdir_y(movespeed+2,direction), sprite_width, obj_player, true, true)
{
//Stop moving
};
Because speed is basically how many pixels per step it's moving, you could plop an imaginary circle down where his destination is going to be on the next step and stop if he predicts a collision within 2 pixels of his sprite size.

The code isn't tested, btw.
 
M

Monster25

Guest
Since he is supposed to follow the player until he gets out of range and switches to idle isin't that the same thing as checking the player's sprite as the collision?
 
B

Blazing

Guest
Sorry, I don't understand what you mean by that question.

Are you asking if collision_circle is pretty much the same as point_distance?
 
M

Monster25

Guest
I mean that the enemy object in my game is suppose to follow the player when he gets in range so his point direction is my player's x and y and im checking the player's sprite for collision when the enemy collides with him. Isin't this the same thing as above? only that I test for the created circle on the player's position rather than the sprite itself?
 
Top