Legacy GM [SOLVED] Checking for Solid Objects in a Line

I

IzzieBlu

Guest
Hello all I have a bit of a problem and I need a some help with it. I'll explain as well as I can.

I'm currently working on a top-down shooter for fun where you can at shoot enemies over furniture and non-solid objects but not through walls and solid objects. In the game when you click (shoot) it creates an instance of the shot object and any enemy object touching that will check if there are any solid objects in between the player and itself. Only problem is that I'm not entirely sure how to do that.
While trying to get this to work I fiddled around with the collision_line() function, but to no avail; the collision_line() function doesn't work how I thought it would (check if there are any solid objects colliding with the line between the two objects or points).

Heres the code I'm working with:
Code:
if(!object_get_solid(collision_line(x, y, obj_player.x, obj_player.y, obj_zombie, 0, 0)) && place_meeting(x, y, obj_shot))
{
    hp-=obj_shot.dmg
    with(obj_shot){ instance_destroy() }
}
cc3e.png

(red = collision/can't damage, green = passes through/can damage)
cc3ee.png

cc3eee.png

cc2e.png

With this code the enemy is damaged no matter what is in the way, even if theres nothing. I have tried all combinations and it only ever resulted in either the enemy always being damaged or the enemy never being damaged (removing the '!', changing the condition to .solid, changing the contition to == noone, etc.).

I've looked all over the place online and I can't seem to find any posts, tutorials or anything useful or very relevant to my problem.

If you have any ideas that would be very helpful and appreciated!
(sorry if I called something incorrect or used wrong terms; I'm self taught)
Thank you for reading~


Bonus / Extra:
As well as that, I was wondering if there would be a way to not allow an enemy behind an enemy to get shot/damaged by the shot.
cc1e.png
If so: is there a way to only draw the shot up to that point? (Currently the shot is simply an image however I've been thinking of changing it to a generated line of sorts)

Since both of these are not entirely needed for the core of the game I don't need them answered; these two questions are simply similar so I added them is as well.
 
Hello all I have a bit of a problem and I need a some help with it. I'll explain as well as I can.

I'm currently working on a top-down shooter for fun where you can at shoot enemies over furniture and non-solid objects but not through walls and solid objects. In the game when you click (shoot) it creates an instance of the shot object and any enemy object touching that will check if there are any solid objects in between the player and itself. Only problem is that I'm not entirely sure how to do that.
While trying to get this to work I fiddled around with the collision_line() function, but to no avail; the collision_line() function doesn't work how I thought it would (check if there are any solid objects colliding with the line between the two objects or points).

Heres the code I'm working with:
Code:
if(!object_get_solid(collision_line(x, y, obj_player.x, obj_player.y, obj_zombie, 0, 0)) && place_meeting(x, y, obj_shot))
{
    hp-=obj_shot.dmg
    with(obj_shot){ instance_destroy() }
}
View attachment 8523

(red = collision/can't damage, green = passes through/can damage)
View attachment 8524

View attachment 8527

View attachment 8526

With this code the enemy is damaged no matter what is in the way, even if theres nothing. I have tried all combinations and it only ever resulted in either the enemy always being damaged or the enemy never being damaged (removing the '!', changing the condition to .solid, changing the contition to == noone, etc.).

I've looked all over the place online and I can't seem to find any posts, tutorials or anything useful or very relevant to my problem.

If you have any ideas that would be very helpful and appreciated!
(sorry if I called something incorrect or used wrong terms; I'm self taught)
Thank you for reading~


Bonus / Extra:
As well as that, I was wondering if there would be a way to not allow an enemy behind an enemy to get shot/damaged by the shot.
View attachment 8522
If so: is there a way to only draw the shot up to that point? (Currently the shot is simply an image however I've been thinking of changing it to a generated line of sorts)

Since both of these are not entirely needed for the core of the game I don't need them answered; these two questions are simply similar so I added them is as well.
Hmmm....I'm not sure but try looking in the Gamemaker Marketplace.
 
I

IzzieBlu

Guest
Update: using lengthdir_x and lengthdir_y are a lot easier than anything else I've found so if you're looking at this in search of something similar try this.

Code:
/// meeting_line( x1, y1, x2, y2, obj )
var x1 = argument[0], y1 = argument[1],
x2 = argument[2], y2 = argument[3],
obj = argument[4];

var temp_x = x1, temp_y = y1,
space = 4, // set space to something not too small so that the collision won't ignore something
dir = point_direction(x1, y1, x2, y2), c = false;

// The collision line checking
while( !place_meeting(temp_x, temp_y, obj) &&
      temp_x>0 && temp_y>0 && temp_x<room_width && temp_y<room_height) // making sure the temp_x and temp_y doesn't go too far (change these to view_xview, view_yview, view_xview+view_wview, view_yview+view_hview if you only need the collision on the screen).
{
      temp_x += lengthdir_x(space, dir)
      temp_y += lengthdir_y(space, dir)
}
if(place_meeting(temp_x, temp_y, obj))
{
      c = true
      // This will make the x and y go back to the last place that was free in case you need this later
      temp_x += lengthdir_x(space, dir+180)
      temp_y += lengthdir_y(space, dir+180)
}

return c;
This method has worked for me in the past so this should work but I haven't tested this exact code.
Hope this helped!
Cheers~
 
Top