我有敌人预制件有海军特工。敌人是由下给脚本控制的,我怎么能用这个脚本有随机的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;
}
}
}发布于 2017-02-21 07:24:41
用UnityEngine.Random.Range(yourMinf, yourMax5f);获取随机数。
将该号码分配给NavMeshAgent.speed。
例如,下面的代码段将在2和5之间分配随机速度。
nav.speed = UnityEngine.Random.Range(2f, 5f);只需将其放入if语句中即可。您还可能对其他变量(如NavMeshAgent.angularSpeed和NavMeshAgent.acceleration )感兴趣。
https://stackoverflow.com/questions/42360056
复制相似问题