• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GML help me with this holy collision code

kureoni

Member
so, in my game, there is mobs that follows you once they are spawned, but since these mobs' angle always face the player position, sometimes they turn their angle and the collision box of the mob gets stuck inside of the collision box of the wall and sometimes, inside of the collision box of each other, is there any code that pushes an object away of the collision box of the other object if it gets stuck or any code that prevents that?

the collision code of the mobs is overall fine, thats the only issue i'm having rn.

GML:
if life>0 && instance_exists(obj_player){
image_angle = point_direction(x, y, obj_player.x, obj_player.y);
mp_potential_step(obj_player.x,obj_player.y,spd,obj_walls && obj_hole)
}
it turn around walls to get the player etc
but sometimes there is this issue with the angle rotating etc
 

HayManMarc

Member
Don't know if it will work with your project, but you can rotate the sprite without rotating the mask by manually drawing the sprite. (Won't work well if you need precise collisions.)
 

Yal

šŸ§ *penguin noises*
GMC Elder
After rotating, check if you're currently colliding with walls. If so, move outside by shifting the instance's position. (A loop over all 360 directions inside a loop of ranges from 1 to <maximal distance you'll allow the enemies to shift> should do the trick - pick the first valid position you find using the distance/angle combo and move there, then abort both loops)
 

Yal

šŸ§ *penguin noises*
GMC Elder
This is legal syntax, now?
It's perfectly legal syntax. The expression evaluates to false if either object is the first one in the resource tree listing (index 0), and true otherwise, thereby making the code avoid the second object in the resource listing (index 1).

...which probably isn't what the OP wanted to do.
 

FrostyCat

Member
You can't use && like that. Make a new object called pobj_obstacle, and make it the parent of both obj_walls and obj_hole, then check collisions against that.
GML:
mp_potential_step(obj_player.x, obj_player.y, spd, pobj_obstacle);
You should also read this article: How NOT to use && and ||
How to Avoid Misusing && and ||
If you don't want to use && and || where they don't belong and not know what hit you, follow these 2 guidelines:
  • DO NOT translate English sentences word-for-word into GML symbols.
  • If either side of an && or || operator is not meant to be treated as a Boolean value (true/false), the code is wrong.
 
Top