我有三个对象,obj_player,obj_enemy,obj_wall。现在,我有来自gmc论坛的代码。
shoot_cooldown -= 1; //Lower the shooting cooldown timer
if (shoot_cooldown < 0) then shoot_cooldown = 0 //Prevents timer from counting down further than 0.
target_distance = distance_to_object(obj_player); //Distance to the player from enemy.
if (target_distance < 64) //If player within the range of enemy
{
//image_angle = point_direction(x,y,obj_player.x,obj_player.y); //Enemy faces the player.
if (shoot_cooldown == 0) //If enemy can shoot (cooldown ready)
{
bullet = instance_create(x,y,obj_bullet); //Create a bullet relative to enemy
bullet.direction = point_direction(x,y,obj_player.x,obj_player.y); //Shoot it towards player
bullet.speed = 3; //Give it speed
shoot_cooldown = 50; //Set the new cooldown time between low and high thresholds.
}
}
这完美无缺。但是,obj_player和obj_enemy的尺寸为64x64。在我的代码中,如果玩家的距离等于或低于64pix,敌人将发射子弹。现在obj_wall的维度为32x32。如果玩家位于墙的另一侧,则敌人不得发射子弹,因为敌人“无法检测”玩家。但敌人仍然发射子弹因为玩家在64像素之内。我想知道是否有一种解决方法可以让敌人在他们之间有一堵墙时停止射击。感谢那些愿意回复的人。我知道有gmc论坛我希望有人也可以帮助我。
答案 0 :(得分:0)
在collision_line
和obj_enemy
之间使用obj_player
,例如
if target_distance < 64 and collision_line(x, y, obj_player.x, obj_player.y, obj_wall, false, true) == noone
{
// shoot
}
为了更完美,你可以检查敌人和玩家的每个角落之间的界线