我正在制作一个波浪生存游戏,您可以在其中建立防御,我想制作它,以便敌人会攻击您放置的物体以破坏它们并攻击您。我想知道是否有办法找到敌人是否没有以一定的速度移动,因此他们被困在墙后面试图接近玩家。如果他们被卡住,用它来让他们攻击最近的墙壁。
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class EnemyMovement : MonoBehaviour
{
// Reference to the player's position.
Transform player;
Transform wall;
// Reference to the player's health.
PlayerHealth playerHealth;
// Reference to this enemy's health.
EnemyHealth enemyHealth;
// Reference to the nav mesh agent.
UnityEngine.AI.NavMeshAgent nav;
void Awake ()
{
// Set up the references.
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
wall = GameObject.FindGameObjectWithTag("Wall").transform;
}
void Update ()
{
// If the enemy and the player have health left...
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
// ... set the destination of the nav mesh agent to the player.
nav.SetDestination(player.position);
}
// Otherwise...
else
{
// ... disable the nav mesh agent.
nav.SetDestination(player.position);
}
}
}
}
我将健康脚本添加到玩家使用的墙壁以及刚体