[英]Unity: Random NavMesh Agent speed C#?
I have Enemy prefab that has navmesh agent. 我有具有navmesh代理的敌人预制件。 The enemy is controlled by the down give script , how can i have random navmesh agent speed using the give script
敌人由down Give脚本控制,我如何使用Give脚本获得随机的navmesh代理速度
Enemy Movement 敌人运动
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;
}
}
}
Get random number with UnityEngine.Random.Range(yourMinf, yourMax5f);
使用
UnityEngine.Random.Range(yourMinf, yourMax5f);
获得随机数UnityEngine.Random.Range(yourMinf, yourMax5f);
. 。
Assign that number to NavMeshAgent.speed
. 将该数字分配给
NavMeshAgent.speed
。
For example, the snippet below will assign random speed between 2
and 5
. 例如,下面的代码片段将分配
2
到5
之间的随机速度。
nav.speed = UnityEngine.Random.Range(2f, 5f);
Just put that inside your if
statement. 只需将其放入
if
语句中即可。 You may also be interested in other variables such as NavMeshAgent.angularSpeed
and NavMeshAgent.acceleration
您可能也对其他变量感兴趣,例如
NavMeshAgent.angularSpeed
和NavMeshAgent.acceleration
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.