首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >统一:随机NavMesh代理速度C#?

统一:随机NavMesh代理速度C#?
EN

Stack Overflow用户
提问于 2017-02-21 06:21:42
回答 1查看 1.1K关注 0票数 0

我有敌人预制件有海军特工。敌人是由下给脚本控制的,我怎么能用这个脚本有随机的navmesh代理速度?

敌人运动

代码语言:javascript
运行
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-21 07:24:41

UnityEngine.Random.Range(yourMinf, yourMax5f);获取随机数。

将该号码分配给NavMeshAgent.speed

例如,下面的代码段将在25之间分配随机速度。

代码语言:javascript
运行
复制
nav.speed = UnityEngine.Random.Range(2f, 5f);

只需将其放入if语句中即可。您还可能对其他变量(如NavMeshAgent.angularSpeedNavMeshAgent.acceleration )感兴趣。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42360056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档