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

Legacy GM [SOLVED] How do I check the direction in which an object is, relative to another object?

Dr_Nomz

Member
I'm trying to make a zombie attack a barricade based on where it is, since just setting it to attack obj_Barricade.x/y just sends it in random directions. (Because there's more than one)

Code:
if (distance_to_object(obj_Barricade_1) < 50){
  var angle1 = point_direction(x,y,obj_Barricade_1.x,obj_Barricade_1.y);
}
I want to make 4 different statements changing the variable based on which direction the barricade is relative to the zombie, like if it's to the right of it, it'll attack RIGHT, and it's above it, it'll attack UP.

But how do I write that in GML?
 

TheouAegis

Member
You haven't learned this yet? We've been discussing this for as long as you've been around the forums. lol I'd assume you'd want the zombie to go for the barricade closest to it. That's simply:

Code:
var target; target = instance_nearest(obj_Barricade_1);
if distance_to_object(target) < 50
    var angle1 = point_direction(x,y,target.x,target.y);
But if you want the zombie to attack the barricade in front of it, maybe something like this:

Code:
var lx = x+lengthdir_x(50,direction), ly = y+lengthdir_y(50,direction), target = noone;
with obj_Barricade_1 {
    if distance_to_object(other) < 50 //You may be able to remove this line, but I added it in case the next line is too slow
        if collision_line(other.x,other.y,lx,ly,id) {
            target = id;
            break;
        }
}
if target != noone
    var angle1 = point_direction(x,y,target.x,target.y);
Add a whole bunch of barricades into the room and then test if it runs better or worse with the distance_to_object(other) condition. I don't remember if collision_line got optimized between versions.
 

Dr_Nomz

Member
The first code works fine, but they focus on the center origin and it looks really weird. The second block you posted is really buggy.

I'd just like it to read if the nearest barricade is either up,down,left or right, and then send it's attack in that direction. How do I do that?
 

Bentley

Member
I'm trying to make a zombie attack a barricade based on where it is, since just setting it to attack obj_Barricade.x/y just sends it in random directions. (Because there's more than one)

Code:
if (distance_to_object(obj_Barricade_1) < 50){
  var angle1 = point_direction(x,y,obj_Barricade_1.x,obj_Barricade_1.y);
}
I want to make 4 different statements changing the variable based on which direction the barricade is relative to the zombie, like if it's to the right of it, it'll attack RIGHT, and it's above it, it'll attack UP.

But how do I write that in GML?
Could use instance nearest to get the barricade, then point_distance to check if it's in range, and then point_direction and lengthdir_x/y to move to it.
 
when you have a specific id for the barricade, you can use (within the zombie):

if barricade id.bbox_bottom < bbox_top
{is above}

if barricade id.bbox_top > bbox_bottom
{is below}

if barricade id.bbox_right < bbox_left
{is left}

if barricade id.bbox_left > bbox_right
{is right}

for some basic sense of where the barricade is in relation to the zombie. By combining those results you can be more specific. Have the zombie move up, or down, if it's not quite aligned with the barricade, or whatever. As long as you have a fixed id for the barricade, then getting into a good position to look natural when attacking it isn't particularly difficult (assuming that the bbox dimensions are fairly tight to the actual area of the sprite) Just probably long winded covering the various permutations.

Or you could do something like:

See if there is a collision with collision_line

If there is then keep shrinking down the length of the line, making note of the collision point until there is no more collision. So then you have the last point where there was a collision.

Which means you have the exact point on the barricade where the zombie should attack, rather than having it "aiming" at the origin point.


One of the tutorials in the tutorials section of the forum called "pixel perfect collision" covers how you can do this.
 

Dr_Nomz

Member
Code:
    var target;
    target = instance_nearest(x,y,obj_Barricade_1);
    if collision_line(x,y,x+50,y,target,0,0){
      var angle1 = point_direction(x,y,x+50,y);
    }else //left
    if collision_line(x,y,x-50,y,target,0,0){
      var angle1 = point_direction(x,y,x-50,y);
    }else //down
    if collision_line(x,y,x,y+50,target,0,0){
      var angle1 = point_direction(x,y,x,y+50);
    }else //up
    if collision_line(x,y,x,y-50,target,0,0){
      var angle1 = point_direction(x,y,x,y-50);
    }else{ //other
      angle1 = point_direction(x,y,target.x,target.y);
    }
Code I used, and it worked. Thanks for the help everyone!
 
Top