當前位置: 首頁>>代碼示例>>C#>>正文


C# NavMeshAgent.CalculatePath方法代碼示例

本文整理匯總了C#中UnityEngine.NavMeshAgent.CalculatePath方法的典型用法代碼示例。如果您正苦於以下問題:C# NavMeshAgent.CalculatePath方法的具體用法?C# NavMeshAgent.CalculatePath怎麽用?C# NavMeshAgent.CalculatePath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在UnityEngine.NavMeshAgent的用法示例。


在下文中一共展示了NavMeshAgent.CalculatePath方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CalculatePathLength

    // Unity's example function..
    public float CalculatePathLength(NavMeshAgent nav, Vector3 targetPosition)
    {
        // Added condition since it was returning impossible values if given same position :P
        if (nav.transform.position == targetPosition)
            return 0;

        // Create a path and set it based on a target position.
        var path = new NavMeshPath();
        if (nav.enabled)
            nav.CalculatePath(targetPosition, path);

        // Create an array of points which is the length of the number of corners in the path + 2.
        var allWayPoints = new Vector3[path.corners.Length + 2];

        // The first point is the player position.
        allWayPoints[0] = player.transform.position;

        // The last point is the enemy position
        allWayPoints[allWayPoints.Length - 1] = targetPosition;

        // The points inbetween are the corners of the path.
        for (var i = 0; i < path.corners.Length; i++)
            allWayPoints[i + 1] = path.corners[i];

        // Create a float to store the path length that is by default 0.
        float pathLength = 0;

        // Increment the path length by an amount equal to the distance between each waypoint and the next.
        for (int i = 0; i < allWayPoints.Length - 1; i++)
        {
            pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);
        }

        return pathLength;
    }
開發者ID:Kullax,項目名稱:competence,代碼行數:36,代碼來源:SavedValues.cs

示例2: InteractWithNearestState

        public InteractWithNearestState(NavMeshAgent agent, string tag, GameObject pickup)
        {
            _agent = agent;

            cam = _agent.gameObject.GetComponent<CharacterAnimMovement>();

            _interactGameObject = pickup;

            var interactables = GameObject.FindGameObjectsWithTag(tag);
            if (interactables.Length < 1)
            {
                GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioManager>().FemaleNoSoundPlay();
                _state = State.Done;
                return;
            }

            foreach (var i in interactables)
            {
                _interactGameObject = i;
                var dest = i.GetComponent<IInteractable>();
                if(!dest.CanThisBeInteractedWith(pickup)) continue;
                var path = new NavMeshPath();
                agent.CalculatePath(dest.InteractPosition(_agent.transform.position), path);

                if (path.status != NavMeshPathStatus.PathComplete) continue;

                _intaractableGoal = dest;
                break;
            }

            if(_intaractableGoal == null)
                _state = State.Done;
        }
開發者ID:veselin-,項目名稱:Team4BabelGame,代碼行數:33,代碼來源:InteractWithNearestState.cs

示例3: Start

 void Start()
 {
     agent = GetComponent<NavMeshAgent>();
     NavMeshPath path = new NavMeshPath();
     agent.CalculatePath(target.position, path);
     if (path.status == NavMeshPathStatus.PathPartial)
     {
     }
 }
開發者ID:briggsoft,項目名稱:Loop,代碼行數:9,代碼來源:Test.cs

示例4: CalcPathDistance

        // Berechnet die Distanz zwischen 2 Vektoren auf einem NavMesh
        public static float CalcPathDistance(Vector3 originPos, Vector3 targetPos, ref NavMeshAgent nav)
        {
            NavMeshPath path = new NavMeshPath();
            if (nav.enabled)
                nav.CalculatePath(targetPos, path);
            Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];
            allWayPoints[0] = originPos;
            allWayPoints[allWayPoints.Length - 1] = targetPos;
            for (int i = 0; i < path.corners.Length; i++)
            {
                allWayPoints[i + 1] = path.corners[i];
            }

            float pathLength = 0;
            for (int i = 0; i < allWayPoints.Length - 1; i++)
            {
                pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);
            }

            return pathLength;
        }
開發者ID:sixeco,項目名稱:Unnoticed,代碼行數:22,代碼來源:Spatial.cs

示例5: EndGameState

        public EndGameState(NavMeshAgent agent)
        {
            _agent = agent;

            var endPoints = GameObject.FindGameObjectWithTag(Constants.Tags.GameMaster).
                GetComponent<EndPoints>().Endpoints;

            if (endPoints.Length < 1)
                Debug.LogError("No interactables in scene, FUCK!");

            foreach (var i in endPoints)
            {
                var path = new NavMeshPath();
                agent.CalculatePath(i.transform.position, path);

                if (path.status != NavMeshPathStatus.PathComplete) continue;

                _destination = i;
                break;
            }

            if (_destination == null)
                _state = State.Done;
        }
開發者ID:veselin-,項目名稱:Team4BabelGame,代碼行數:24,代碼來源:EndGameState.cs


注:本文中的UnityEngine.NavMeshAgent.CalculatePath方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。