【问题标题】:Unity: Random NavMesh Agent speed C#?Unity:随机导航网格代理速度 C#?
【发布时间】:2017-02-21 06:21:42
【问题描述】:

我有 Enemy 预制件,它有 navmesh 代理。敌人是由down give脚本控制的,我怎样才能使用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;
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

    将该号码分配给NavMeshAgent.speed

    例如,下面的 sn-p 将在 25 之间分配随机速度。

    nav.speed = UnityEngine.Random.Range(2f, 5f);
    

    只需将其放在您的 if 声明中即可。您可能还对其他变量感兴趣,例如 NavMeshAgent.angularSpeedNavMeshAgent.acceleration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-26
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多