我的敌方预制剂具有navmesh代理。敌人由下给脚本控制,我如何使用give脚本
才能使随机的navmesh代理速度敌人运动
public class EnemyMovement : MonoBehaviour
{
Transform player; // Reference to the player's position.
PlayerHealth playerHealth; // Reference to the player's health.
EnemyHealth enemyHealth; // Reference to this enemy's health.
UnityEngine.AI.NavMeshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
}
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.enabled = false;
}
}
}
用 UnityEngine.Random.Range(yourMinf, yourMax5f);
获取随机数。
将该号码分配给NavMeshAgent.speed
。
例如,下面的摘要将分配2
和5
之间的随机速度。
nav.speed = UnityEngine.Random.Range(2f, 5f);
只需将其放入您的if
语句中即可。您可能也对其他变量感兴趣,例如NavMeshAgent.angularSpeed
和NavMeshAgent.acceleration