当前位置: 首页>>代码示例>>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;未经允许,请勿转载。