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

[SOLVED] AI won't move after collision event is triggered once

M

MyDude

Guest
the problem I've been having is that the AI won't move at all after it has collided with the wall (the AI isn't stuck in the wall)

What I've Noticed
The problem only happens when I try to get the AI to move away from the player and here is the code for that
the vspd is affecting the hspd for some reason

eR is shorthand for Enemy Ranged
Code:
///eR_collide

//makes him run away

    //Horizontal collision
if (place_meeting(x + hspd, y, obj_solid)) {
    while (!place_meeting(x + sign(hspd), y, obj_solid)) {
        x+=sign(hspd);
    }
    hspd = 0;     //works fine for me
}
x -= hspd;

    //Vertical collisions
if (place_meeting(x, y + vspd, obj_solid)) {
    while (!place_meeting(x, y + sign(vspd), obj_solid)) {
        y += sign(vspd);     
    }
    vspd = 0;    //when this code gets ran it sets my hspd = 0
}
y -= vspd;


it's pretty simple but the way I'm making him run away from the player is by changing how vspd and hspd are being added, so instead of it being += I changed it -=. Problem is it breaks the collision detection and he can't move at all once he's collided on the y axis.

Here is the rest of the code that has to do with collisions

Ranged enemy's run away state
Code:
///scr_eR_run_away()

var dis = point_distance( x, y, obj_player.x, obj_player.y);
dir = point_direction(x, y, obj_player.x, obj_player.y);
hspd = lengthdir_x( spd, dir);
vspd = lengthdir_y( spd, dir);

if (hspd != 0) {
    image_xscale = sign(hspd);
}
eR_collide();     //the script that's handling collisions and moving the enemy

if (round(dis) > 400) {
    //make him stop moving
    spd = 0
} else if (round(dis) < 400) {
    spd = 2;
}

Ranged Enemy's Create Event
Code:
///variables

spd = 2;
state = scr_eR_run_away;

Step is just a Script Execute

And I added this to fix a problem I had when flipping the xscale so the AI wouldn't get stuck in the wall

In the Ranged Enemy theres is a collision event with Obj_Solid with this code:
Code:
check = collision_rectangle(other.bbox_left + 10, other.bbox_top + 10, other.bbox_right - 10, other.bbox_bottom - 10, obj_caster, false, false);
if (check == noone) {
 }
 
if (check != noone) {
    if (image_xscale == 1) {   
        if (x > other.bbox_left) {
            x += 18;
        }
        if (x < other.bbox_right) {
            x -= 18;
        }
    }
    
     if (image_xscale == -1) {
        if (x > other.bbox_left) {
            x += 18;
        }
        if (x < other.bbox_right) {
            x -= 18;
        }
     }
}

This may be irrelevant but it might help to post the code that does work for me in terms of collisions(the one my players using)

Player's Working Collision (it seems that way at least)
Code:
///collide()

//Horizontal collision
if (place_meeting(x + hspd, y, obj_solid)) {
    while (!place_meeting(x + sign(hspd), y, obj_solid)) {
        x+=sign(hspd);
        
    }
    hspd = 0;
}
x += hspd;

//Vertical collisions
if (place_meeting(x, y + vspd, obj_solid)) {
    while (!place_meeting(x, y + sign(vspd), obj_solid)) {
        y += sign(vspd);
        
    }
    vspd = 0;
}
y += vspd;

//checks to see if the player is stuck and if he is move him 10 pixels so he isn't stuck anymore
if (place_meeting(x, y, obj_solid)) {
    if (image_xscale == 1) {
    show_debug_message("stuck")
    x += 10;
    }
    
    if (image_xscale == -1) {
    show_debug_message("not stuck")
    x -= 10; //prevent();
    }
}
If I confused you(I have a weird way of explaining things sorry) or you need more information to help me please let me know so I can add it.

Any help would be appreciated and if it isn't much to ask an explanation on how the solution would work/ why I'm gettting the bug I'm currently getting.
 

TheouAegis

Member
If you're going to check for collisions in front and then move backward, then what's the point in checking for collisions to the front? Check for collisions in the direction of movement - if you're moving backwards, check for collisions behind.
 
M

MyDude

Guest
If you're going to check for collisions in front and then move backward, then what's the point in checking for collisions to the front? Check for collisions in the direction of movement - if you're moving backwards, check for collisions behind.
Wanted to thank you for helping me realize what I did wrong it fixed my issue. Sorry I'm still pretty new at this.
 
Top