Line of sight

A

Ajay Warden

Guest
I don't want an enemy to shoot at me when an object is between it and me.

So far I have this,

var CanSee = !collision_line(x, y, obj_player.x, obj_player.y, obj_barn, false, true);
var IsInsideRadius = distance_to_object(obj_player) <= 500;

if(CanSee and IsInsideRadius){
var inst;
inst = instance_create(x+lengthdir_x(65, image_angle), y+lengthdir_y(65,image_angle), obj_enemybullet);
with (inst)
{
speed = 4;
direction = other.image_angle;
}
enemycanfire = false;
}

That works but only for obj_barn, how would I add obj_house to this also so the enemy doesn't shoot me when obj_barn or obj_house is in the way?
 

DukeSoft

Member
You could use parenting for this, by making obj_barn and obj_house a child of "obj_solid" or something like that.

The other method (not so code-efficient) is to do like this;

Code:
var CanSee = !collision_line(x, y, obj_player.x, obj_player.y, obj_barn, false, true); 
if (CanSee) {
    CanSee = !collision_line(x, y, obj_player.x, obj_player.y, obj_house, false, true); 
}
andsoforth.

This isn't really efficient, so you could make a script for it;

Code:
///script scr_canSee(targetx, targety);
if (collision_line(x, y, argument0, argument1, obj_barn, false, true)) {
    return false;
}

if (collision_line(x, y, argument0, argument1, obj_house, false, true)) {
    return false;
}

return true;
Then use it like this;
Code:
var CanSee = scr_canSee(obj_player.x, obj_player.y);
But better is to add barn and house to one parent and check for that one :)
 
A

Ajay Warden

Guest
You could use parenting for this, by making obj_barn and obj_house a child of "obj_solid" or something like that.

The other method (not so code-efficient) is to do like this;

Code:
var CanSee = !collision_line(x, y, obj_player.x, obj_player.y, obj_barn, false, true);
if (CanSee) {
    CanSee = !collision_line(x, y, obj_player.x, obj_player.y, obj_house, false, true);
}
andsoforth.

This isn't really efficient, so you could make a script for it;

Code:
///script scr_canSee(targetx, targety);
if (collision_line(x, y, argument0, argument1, obj_barn, false, true)) {
    return false;
}

if (collision_line(x, y, argument0, argument1, obj_house, false, true)) {
    return false;
}

return true;
Then use it like this;
Code:
var CanSee = scr_canSee(obj_player.x, obj_player.y);
But better is to add barn and house to one parent and check for that one :)
Parenting worked great, I knew there would be a simple solution. Could be I so bold to ask you to take a look at this tread too? You seem to be able to come up with easy solutions https://forum.yoyogames.com/index.php?threads/enemy-tank-problem.13168/
 
Top