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

Player to Object Collision Issue pt.2

O

O_Steven_007

Guest
Hello Im looking for help on a player to object issue. I am currently testing the player to enemy collision and each time I run the game the enemy Character collision box does not register and instead ignores collisions and runs right on top of the player. I do not know what the problem is as I am following a tutorial word for word.
Here is the enemy Step event that I am using for tracking the player and registering collisions.
Code:
/// @description Insert description here

TargetX = obj_player.x-x;
TargetY = obj_player.y-y;

var _targetX = sign(TargetX);
var _targetY = sign(TargetY);

if  (place_meeting(x  + _targetX, y, obj_collision))
{
    while (!place_meeting(x+sign(_targetX), y, obj_collision))
     {
         x += sign(_targetX);
     }
    _targetX = 0;
}
x+=_targetX;

if (place_meeting(x, y + _targetY, obj_collision))
{
    while (!place_meeting(x, y +sign(_targetY), obj_collision))
    {
        y += sign(_targetY);
    }
    _targetY = 0;
}
y+=_targetY;
 

TailBit

Member
It does not check for collision with player, only for its position .. unless player have obj_collision as a parent?

Code:
if  (place_meeting(x  + _targetX, y, obj_collision)){
// here you check for _targetX .. because of sign it can only be 1,0 or -1 ..

   while (!place_meeting(x+sign(_targetX), y, obj_collision)){
   // then this will already be false, because the same check had to be true in the line above
        x += sign(_targetX);
    }
   _targetX = 0;
}
x+=_targetX;
Code:
// usually this code is to check if you can go a distance without collision:
if(place_meeting(x+hspeed,y,obj_collision)){

    // if there is something in front then stop go as close as you can:
    while(!place_meeting(x+sign(hspeed),y,obj_collision)) x += sign(hspeed);

    hspeed = 0;
}
// hspeed is build in, so it will be added to x by itself
It's not often I see that code in the enemy object, then it's more common to use some sort of mp_ code (motion planning)
 
O

O_Steven_007

Guest
Im so sorry I forgot to thank you. The information that you gave me was really helpful. I got it to work based off your suggestion on making obj_collision the parent. However, I am not sure if this is something that I can do throughout the entirety of the project. Is it ok for me to use obj_collision as the parent for more than 5 objects within the project or is there an easier way to make sure that, in the future, objects do not need to be children of obj_collision to collide with the player?
 

TailBit

Member
The tutorial you followed made you end up with a pretty strange code ^^::

This is just a matter of distance, so use: point_distance

for movement directly towards and avoiding solids, you can use:
Code:
mp_linear_step(obj_player.x, obj_player.y, 1, false); // targetx, targety, stepsize, checkAll/solidOnly
If you want smarter movement then I would recommend looking up mp_grid_

So something like:
Code:
if(far distance to player){
    go towards player
}else{
    stop and do something else .. attack?
}
Player shouldn't have to be obj_collision .. there are however no problem using a parent for many objects

Using parents are great, I have one as base for all my moving instances (base variables, gravity and movement handling), and one for all wall objects, which is then split into 2 groups, solid and semi solid platforms (soft)
 
Top