Collision error

Kriit

Member
I Tried making a function for collision for an enemy Object. The horizontal collision works fine, but verticaly the enemy just drops through the walls.
Code:
function scr_move_and_collide(HSpeed,VSpeed){
if !place_meeting(x+HSpeed,y,oWall and oPlayer)
    {
        x+=HSpeed;
    }
if !place_meeting(x,y+VSpeed,oWall and oPlayer)
{
    y+=VSpeed;
}
}
Code:
image_xscale = sign(oPlayer.x - oDemon.x);
    if image_xscale == 0
    {
    image_xscale = 1;    
    }
    var distance_to_player = point_distance(oPlayer.x,oPlayer.y,oDemon.x,oDemon.y);
    if distance_to_player < 100
    {
        scr_move_and_collide(image_xscale*3,0+global.grv)
    }
Additionally global.grv has a value of 0.15 but the enemyObject falls slower than the PlayerObject

Thanks in Advance
 

TailBit

Member
oWall and oPlayer
If any of them got the index 0 then it will result in false, if not then it will be true.. you should not check for collission with true or false.

You have to use 2 place_meeting checks, one for wall and one for player

global.grv isn't used in those codes
 

Kriit

Member
If any of them got the index 0 then it will result in false, if not then it will be true.. you should not check for collission with true or false.

You have to use 2 place_meeting checks, one for wall and one for player

global.grv isn't used in those codes
Thanks, tho it doesn't work either way. I also forgot to mention that its supposed to be a move and collision function.
 

rytan451

Member
GML:
function scr_move_and_collide(HSpeed,VSpeed) {
    if (!place_meeting(x+HSpeed,y,oWall) and !place_meeting(x+HSpeed,y,oPlayer))
        {
        x+=HSpeed;
        }
    if (!place_meeting(x,y+VSpeed,oWall) and !place_meeting(x,y+VSpeed,oPlayer))
        {
        y+=VSpeed;
        }
}
This is what @TailBit was talking about, and it should work. Granted, it isn't perfect, since the method you're using to handle collision isn't that great, but it would work.
 
Top