敌人射击射击范围内最近的物体



我希望敌人(怪物(射击敌人射击场内的玩家,并返回射击场内距离最近的玩家我是Unity的新手,目前正在使用Unity 2017,2D。非常感谢你们的帮助!

公共类怪物:MonoBehavior{

[SerializeField]
GameObject Bullet;
public float fireRate;
float nextFire;
// Use this for initialization
void Start () {
fireRate = 1f;
nextFire = Time.time;
}
// Update is called once per frame
void Update () {
CheckIfTimeToFire ();
}
void CheckIfTimeToFire()
{
if (Time.time > nextFire) 
{
Instantiate (Bullet, transform.position, Quaternion.identity);
nextFire = Time.time + fireRate;
}

}

}

这是项目符号脚本

公共类项目符号:MonoBehavior{

public float moveSpeed = 3f;
Rigidbody2D rb;
PlayerAI target;
Vector2 moveDirection;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
target = GameObject.FindObjectOfType<PlayerAI>();
moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
rb.velocity = new Vector2 (moveDirection.x, moveDirection.y);
Destroy (this.gameObject, 2);
}
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.name.Equals ("Player")) 
{
Debug.Log ("Hit!");
Destroy (gameObject);
}
}

}

假设您有少量玩家,一个方便的方法是将对玩家的引用作为集合,并迭代以获得每个玩家的transform.position。然后你可以检查到怪物的距离以获得最小距离。

我还建议对怪物而不是子弹进行逻辑检查,并将目标传给子弹。这样,如果没有玩家在射程内,你可以选择不实例化子弹。

public class Monster : MonoBehaviour
{
...
public float range;
private PlayerAI[] players;

...
private void Awake()
{
players = FindObjectsOfType<PlayerAI>();
}
...
private void CheckIfTimeToFire()
{
if (Time.time < nextFire)
{
return;
}

PlayerAI closestPlayer = null;
float closestPlayerDistance = float.MaxValue;
foreach (PlayerAI player in players)
{
float playerDistance = Vector3.Distance(transform.position, player.transform.position);
if (playerDistance > range ||
playerDistance > closestPlayerDistance)
{
// Skip to next player.
continue;
}

closestPlayer = player;
closestPlayerDistance = playerDistance;
}
if (closestPlayer == null)
{
// No player in range. Do nothing.
return;
}

GameObject bullet = Instantiate (Bullet, transform.position, Quaternion.identity);
bullet.GetComponent<Bullet>().Initialize(closestPlayer.transform);
nextFire = Time.time + fireRate;
}
...
}
public class Bullet : MonoBehaviour
{
...
private Transform target;
...

public void Initialize(Transform target)
{
this.target = target;
}

...
}

在这个例子中,我得到了Monster的Awake()上的玩家,但如果你在运行时实例化他们,你可以在一个单独的管理器类中保存PlayerAI集合。在这种情况下,列表可能更有用。

public void PlayerManager : MonoBehaviour
{
public List<PlayerAI> Players { get; private set; }

...
private void Awake()
{
while (playerCount < totalPlayerCount)
{
SpawnPlayer();
}
}
private void SpawnPlayer()
{
PlayerAI player = Instantiate(player, ...);
Players.Add(player);
}
// Remove a dead player, etc.
private void OnPlayerDied(PlayerAI player)
{
Players.Remove(player);
}
...
}
public class Monster : MonoBehaviour
{
...
private PlayerManager playerManager;

...
private void Awake()
{
playerManager = FindObjectOfType<PlayerManager>();
}
void CheckIfTimeToFire()
{
...
foreach (PlayerAI player in playerManager.Players)
{
...
}
...
}
}

相关内容

  • 没有找到相关文章

最新更新