Legacy GM [SOLVED] Need help with collision_line

V

VR #456

Guest
I need this function for my turret object to prevenet it from aiming and shooting at player, when they are behind the wall.

All codes in turret object:
Create
Code:
image_speed = 0.5;
alarm[0] = 24;
Alarm 0
Code:
var bullet = instance_create(x, y, obj_projectile);
with (bullet)
    {
       direction = point_direction(x, y, obj_player.x, obj_player.y);
       speed = 7;
    }
alarm[0] = 24;
Step
Code:
image_angle = point_direction(x,y,obj_player.x,obj_player.y);
I tried adding 'if not collision_line(...)' in the beginning of Alarm 0 code, but it resulted in cannon completely stopping shooting after player got behind the wall at least once. Probably I'm missing something?
 

jo-thijs

Member
I need this function for my turret object to prevenet it from aiming and shooting at player, when they are behind the wall.

All codes in turret object:
Create
Code:
image_speed = 0.5;
alarm[0] = 24;
Alarm 0
Code:
var bullet = instance_create(x, y, obj_projectile);
with (bullet)
    {
       direction = point_direction(x, y, obj_player.x, obj_player.y);
       speed = 7;
    }
alarm[0] = 24;
Step
Code:
image_angle = point_direction(x,y,obj_player.x,obj_player.y);
I tried adding 'if not collision_line(...)' in the beginning of Alarm 0 code, but it resulted in cannon completely stopping shooting after player got behind the wall at least once. Probably I'm missing something?
You were probably doing it correctly, exept that this line:
Code:
alarm[0] = 24;
has to stay outside the if-body,
it hould always be executed at the end of the alarm event, regardless of whether the player is in sight.
 
V

VR #456

Guest
You were probably doing it correctly, exept that this line has to stay outside the if-body, it should always be executed at the end of the alarm event, regardless of whether the player is in sight.
Yes, it works now. Thanks!

Since rules require showing the solution, here's the code:
Code:
if not collision_line(x, y, obj_player.x, obj_player.y, obj_wall, 1, 0) {
var bullet = instance_create(x, y, obj_projectile);
with (bullet)
    {
       direction = point_direction(x, y, obj_player.x, obj_player.y);
       speed = 7;
    }
}
alarm[0] = 24;
 
Top