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


C# UnityEngine.NavMeshPath類代碼示例

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


NavMeshPath類屬於UnityEngine命名空間,在下文中一共展示了NavMeshPath類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CalculatePathLength

    float CalculatePathLength(Vector3 targetPosition)
    {
        // Create a path and set it based on a target position.
        NavMeshPath 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.
        Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];

        // The first point is the enemy's position.
        allWayPoints[0] = transform.position;

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

        // The points inbetween are the corners of the path.
        for (int 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:Defsv,項目名稱:UJelly-ProtoType1,代碼行數:32,代碼來源:EnemySight.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: DoCalculatePath

        void DoCalculatePath()
        {
            _getNavMeshPathProxy();
            if (_NavMeshPathProxy ==null)
            {
                return;
            }

            NavMeshPath _path = new NavMeshPath();

            bool _found = NavMesh.CalculatePath(sourcePosition.Value,targetPosition.Value,passableMask.Value,_path);

            _NavMeshPathProxy.path = _path;

            resultingPathFound.Value = _found;

            if (_found)
            {
                if ( ! FsmEvent.IsNullOrEmpty(resultingPathFoundEvent) ){
                    Fsm.Event(resultingPathFoundEvent);
                }
            }else
            {
                if (! FsmEvent.IsNullOrEmpty(resultingPathNotFoundEvent) ){
                    Fsm.Event(resultingPathNotFoundEvent);
                }
            }
        }
開發者ID:KnightsWhoSaysNi,項目名稱:feuerbeuch,代碼行數:28,代碼來源:NavMeshCalculatePath.cs

示例4: OnEnter

    public override void OnEnter(ref AiParam _param)
    {
        if (_param == null || _param.NavAgent == null)
            return;

        _param.OnAiActionChanged (UnityChan_Ctrl.ActionState.Walk);

        //_param.NavAgent.Resume ();
        Movement (ref _param);

        int searchMax = 30;

        for (var i = 0; i < searchMax; i++)
        {
            Vector3 randomPoint = _param.Owner.transform.position + Random.insideUnitSphere * 3F;
            NavMeshHit hit;
            if (NavMesh.SamplePosition (randomPoint, out hit, 1F, 0xFF)) {

                NavMeshPath path = new NavMeshPath();
                if(_param.NavAgent.CalculatePath(hit.position, path) == true)
                {
                    _param.NavAgent.SetPath(path);
                    return;
                }
            }
        }
    }
開發者ID:DeanLu,項目名稱:SnowBall,代碼行數:27,代碼來源:AiStrategy_Patrol.cs

示例5: DrawPath

		void DrawPath(NavMeshPath path) {
			if (path.corners.Length < 2)
				return;
			switch (path.status) {
			case NavMeshPathStatus.PathComplete:
					c = Color.white;
					break;
			case NavMeshPathStatus.PathInvalid:
					c = Color.red;
					break;
			case NavMeshPathStatus.PathPartial:
					c = Color.yellow;
					break;
			}
					
			Vector3 previousCorner = path.corners[0];
			
			int i = 1;
			while (i < path.corners.Length) {
				Vector3 currentCorner = path.corners[i];
				Debug.DrawLine(previousCorner, currentCorner, c);
				previousCorner = currentCorner;
				i++;
			}
	
	
	
		}
開發者ID:tchrisbaker,項目名稱:Unity3DRPG,代碼行數:28,代碼來源:PathUtilsFSMAction.cs

示例6: Update

    void Update()
    {
        /*if (oldpos != transform.position) {
            oldpos = newpos;
            newpos = transform.position;
        } else {
            anim.SetBool("isWalking", false);
        }*/

        if (Input.touchCount == 1)
        {
            Vector3 moveTo = GetPositionTouch();

            //player.SetDestination(moveTo);
            //anim.SetBool("isWalking", true);

            NavMeshPath path = new NavMeshPath();
            GetComponent<NavMeshAgent>().CalculatePath(moveTo, path);

            //Debug.Log(path.status);

            if (path.status == NavMeshPathStatus.PathComplete)
            {
                TouchIndicator(moveTo);
                player.SetDestination(moveTo);
                anim.SetBool("isWalking", true);
            }
        }

        //Debug.Log (player.remainingDistance);

        if (player.remainingDistance == 0) {
            anim.SetBool("isWalking", false);
        }
    }
開發者ID:jsimoesjr,項目名稱:Unity,代碼行數:35,代碼來源:PlayerControl.cs

示例7: Start

 // Use this for initialization
 void Start()
 {
     destination = transform.position;
     path = new NavMeshPath();
     CalcPath();
     rigidBody = GetComponent<Rigidbody>();
 }
開發者ID:SteveRodkiss,項目名稱:PauseTimeStrategy,代碼行數:8,代碼來源:HoverTankFollowPath.cs

示例8: CalculatePathLength

    float CalculatePathLength(Vector3 targetPosition)
    {
        //To calculate how far the sound can travel - identifies player behind corners
        NavMeshPath path = new NavMeshPath();

        if (nav.enabled)
            nav.CalculatePath(targetPosition, path);

        Vector3[] allWayPoints = new Vector3[path.corners.Length + 2]; //+2 to allow for the enemy and player positions

        allWayPoints[0] = transform.position;
        allWayPoints[allWayPoints.Length - 1] = targetPosition;

        for (int i = 0; i < path.corners.Length; i++)
        {
            allWayPoints[i + 1] = path.corners[i];
        }

        float pathLength = 0f;

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

        return pathLength;
    }
開發者ID:rnabeth,項目名稱:stealth,代碼行數:27,代碼來源:EnemySight.cs

示例9: CalculatePathLength

    // this is used for finding how long a sound has to travel
    float CalculatePathLength(Vector3 targetPosition)
    {
        NavMeshPath path = new NavMeshPath();
        if (nav.enabled)
            nav.CalculatePath(targetPosition, path);

        Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];

        //first point in array should be the enemy's posiotn

        allWayPoints[0] = transform.position;
        allWayPoints[allWayPoints.Length - 1] = targetPosition;

        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:daxaxelrod,項目名稱:Loom-Vr,代碼行數:27,代碼來源:npcSight.cs

示例10: DrawLinesWithNavigation

//draw lines using navmesh
void DrawLinesWithNavigation()
{

if(useOwnNavSystem == false)
{
int testId2 = 0;
int testIdPlus2 = 1;


List<Vector3> waypointVector3 = new List<Vector3>();


while(testId2 < waypointList.Length)
{

if(testIdPlus2 >= waypointList.Length)
{
testIdPlus2 = 0;
}
		
NavMeshPath pathMain = new NavMeshPath();

NavMesh.CalculatePath(waypointList[testId2].transform.position, waypointList[testIdPlus2].transform.position, -1, pathMain);


int testId3 = 0;

while(testId3 < pathMain.corners.Length)
{
waypointVector3.Add(pathMain.corners[testId3]);
testId3 += 1;
}

testId2 += 1;
testIdPlus2 += 1;
}


//draw the lines

int testId = 0;
int testIdPlus = 1;

while(testId < waypointVector3.Count)
{

if(testIdPlus >= waypointVector3.Count)
{
testIdPlus = 0;
}

Gizmos.color = Color.magenta;
Gizmos.DrawLine(waypointVector3[testId], waypointVector3[testIdPlus]);

testId += 1;
testIdPlus += 1;
}
}

}
開發者ID:PlayHopeless,項目名稱:game,代碼行數:61,代碼來源:PatrolManager.cs

示例11: FindNearest

        //returns the nearest portal of a container, based on the position
        static Vector3? FindNearest(Transform parent, Vector3 pos)
        {
            //initialize variables
            NavMeshPath path = new NavMeshPath();
            Vector3? nearest = null;
            float distance = Mathf.Infinity;

            //don't continue if the parent does not exist
            if (!portals.ContainsKey(parent)) return null;

            //loop over portals of this portal container,
            //find the shortest NavMesh path to a portal
            for (int i = 0; i < portals[parent].Count; i++)
            {
                Vector3 portal = portals[parent][i].position;
                float length = Mathf.Infinity;
                //let Unity calculate the path and set length, if valid
                if (NavMesh.CalculatePath(pos, portal, -1, path)
                    && path.status == NavMeshPathStatus.PathComplete)
                    length = PathLength(path);

                if (length < distance)
                {
                    distance = length;
                    nearest = portal;
                }
            }

            return nearest;
        }
開發者ID:kostya05,項目名稱:TestingRacing,代碼行數:31,代碼來源:PortalManager.cs

示例12: Start

 void Start()
 {
     anim = GetComponent<Animator>();
     anim.SetBool("isWalking", true);
     NavMeshPath path = new NavMeshPath();
     agent.destination = target.position;
 }
開發者ID:KickAss42,項目名稱:Projet-SurvivAll,代碼行數:7,代碼來源:MobMouve.cs

示例13: Awake

 public virtual void Awake()
 {
     nav = GetComponent<NavMeshAgent> ();
     vision = GetComponent<Vision> ();
     player = GameObject.FindGameObjectWithTag ("Player").transform;
     path = new NavMeshPath ();
 }
開發者ID:Time14,項目名稱:Trapped,代碼行數:7,代碼來源:BaseAI.cs

示例14: findNextDestination

    public void findNextDestination()
    {
        NavMeshPath path = new NavMeshPath();
        int indexTargetChosen = -1;
        bool found = false;
        int count = 0;
        if (getNumberOfTargetsOff() > 0)
        {
            while (!found && count < 99)
            {
                indexTargetChosen = Random.Range(0, targets.Length);
                if (!targets[indexTargetChosen].isOn)
                {
                    m_Agent.CalculatePath(targets[indexTargetChosen].transform.position, path);
                    if (path.status == NavMeshPathStatus.PathComplete)
                    {
                        found = true;
                    }
                }
                count++;
            }
        }

        if (found)
        {
            Vector3 destination = targets[indexTargetChosen].transform.position;
            m_Agent.destination = new Vector3(destination.x, transform.position.y, destination.z);
        }
    }
開發者ID:simonchauvin,項目名稱:Ludum-Dare-33,代碼行數:29,代碼來源:Player.cs

示例15: Update

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitinfo;

           // LayerMask mask = 1 << LayerMask.NameToLayer("GroundLayer");

            bool isCollider = Physics.Raycast(ray, out hitinfo);
           
            if (isCollider && hitinfo.collider.tag == Tags.player)
            {
                cur_Role = hitinfo.collider.transform.parent.GetComponent<DBaseFightRole>();               
            }
            if (isCollider && hitinfo.collider.tag == Tags.ground && cur_Role != null)
            {
                NavMeshPath path = new NavMeshPath();
                cur_Role.agent.enabled = true;
                cur_Role.agent.CalculatePath(hitinfo.point, path);
                if (path.corners.Length >= 2)
                {
                    cur_Role.gotoDestination(hitinfo.point);
                   
                }
                cur_Role = null;
            }
        }
    }
開發者ID:satela,項目名稱:xjhU3d,代碼行數:29,代碼來源:CastSkill.cs


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