1

我有三个对象,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 论坛我希望有人也可以在这里帮助我。

4

1 回答 1

0

在andcollision_line之间使用,例如obj_enemyobj_player

if target_distance < 64 and collision_line(x, y, obj_player.x, obj_player.y, obj_wall, false, true) == noone
{
    // shoot
}

为了更完美,您可以检查敌人和玩家每个角落之间的线

于 2014-01-25T03:28:12.987 回答