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

GML [SOLVED] Shooting diagonally with collision_line

jujubs

Member
Hi everyone. So I've been struggling with this for the last few hours, and after failing to find a solution elsewhere, I've decided to ask here: how do I check a collision with collision_line in other directions?

Cardinal directions are easy, because it uses a rectangle, but I can't find a way to do it at a 45 degree angle, for instance.

This is my code:
Code:
if(collision_line(obj_player1.x+16,obj_player1.y-1,obj_player1.x+1000,obj_player1.y+1, obj_enemy, true, false))
    {
        instance_destroy();
    }
How exactly would I go about this?

I've read about lengthdir, but haven't found out how to actually implement it :/
 

Simon Gust

Member
lengthdir is the right function.
Here ->
length: 1000 pixel
angle: 45°
origin: x+16, y
Code:
var x1 = obj_player1.x+16;
var y1 = obj_player1.y;
var x2 = x1 + lengthdir_x(1000, 45);
var y2 = y1 + lengthdir_y(1000, 45);
if (collision_line(x1, y1, x2, y2, obj_enemy, true, false))
{
   instance_destroy();
}
 
Top