我正在制作一个海浪生存游戏,您可以在其中建立防御力,我想做到这一点,以便敌人可以攻击您放下的物体以破坏它们并攻击您。我想知道是否有办法找到敌人是否没有以一定速度移动,因此他们被困在墙后试图到达玩家手中。如果卡住了,用它来使它们攻击最近的墙。
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);
}
}
}
}
我将健康脚本添加到播放器上使用的墙壁以及刚体上
答案 0 :(得分:0)
假设您的敌人身上附着有刚体,则可以使用刚体的速度分量的大小来确定其速度。有了这个,你可以做类似...
Rigidbody2D rbody;
void Start()
{
rbody = GetComponent<Rigidybody2D>();
}
void Update()
{
if (rbody.velocity.magnitude < 1f)
{
// change target to wall or do some logic here
}
else
{
// target is player,
}
}