统一:随机NavMesh代理速度C#?
我有一个有navmesh特工的敌人预制场。敌人被下交脚本控制,如何使用下交脚本获得随机navmesh代理速度 敌人的行动统一:随机NavMesh代理速度C#?,c#,unity3d,C#,Unity3d,我有一个有navmesh特工的敌人预制场。敌人被下交脚本控制,如何使用下交脚本获得随机navmesh代理速度 敌人的行动 public class EnemyMovement : MonoBehaviour { Transform player; // Reference to the player's position. PlayerHealth playerHealth; // Reference to the player's heal
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;
}
}
}
public class EnemyMovement:单一行为
{
Transform player;//对播放器位置的引用。
PlayerHealth PlayerHealth;//对玩家健康的引用。
EnemyHealth EnemyHealth;//引用此敌人的健康。
UnityEngine.AI.navMesh代理导航;
无效唤醒()
{
player=GameObject.FindGameObjectWithTag(“player”).transform;
playerHealth=player.GetComponent();
enemyHealth=GetComponent();
nav=GetComponent();
}
无效更新()
{
//如果敌人和玩家还有生命值。。。
如果(enemyHealth.currentHealth>0&&playerHealth.currentHealth>0)
{
//…将导航网格代理的目标设置为玩家。
导航设置目的地(玩家位置);
}
//否则。。。
其他的
{
//…禁用导航网格代理。
nav.enabled=false;
}
}
}
使用UnityEngine.random.Range(yourMinf,yourMax5f)获取随机数代码>
把那个号码分配给我
例如,下面的代码段将在2
和5
之间分配随机速度
nav.speed = UnityEngine.Random.Range(2f, 5f);
只要把它放在你的
if
语句中就行了。您可能还对其他变量感兴趣,例如和使用UnityEngine.random.Range(yourMinf,yourMax5f)获取随机数代码>
把那个号码分配给我
例如,下面的代码段将在2
和5
之间分配随机速度
nav.speed = UnityEngine.Random.Range(2f, 5f);
只要把它放在你的if
语句中就行了。您可能还对其他变量感兴趣,例如和