// Update is called once per frame void Update() { if (navMeshAgent.remainingDistance <= arrivalDistance) { SelectRandomDestination(); } navMeshAgent.CalculatePath(destination, navMeshPath); navMeshAgent.SetPath(navMeshPath); }
public void MoveByScreenPoint(Vector3 position) { Ray ray = m_camera.ScreenPointToRay(position); RaycastHit hit; if (Physics.Raycast(ray, out hit, 999, m_groundLayer)) { UnityEngine.AI.NavMesh.CalculatePath(m_agent.transform.position, hit.point, -1, m_path); m_agent.SetPath(m_path); } }
public void MoveTo(Vector3 location) { if (!location.Equals(Vector3.zero)) { // currentVector = location; // agent.SetDestination(currentVector); NavMeshPath path = new NavMeshPath(); agent.CalculatePath(location, path); agent.SetPath(path); RpcMoveTo(location); } }
private Vector3 getRandomPosition() { Vector3 randomPosition; BoxCollider boxCollider = area != null?area.GetComponent <BoxCollider>() : null; if (boxCollider != null) { NavMeshPath path = new NavMeshPath(); int numIterations = 0; do { do { randomPosition = new Vector3(UnityEngine.Random.Range(area.transform.position.x - area.transform.localScale.x * boxCollider.size.x * 0.5f, area.transform.position.x + area.transform.localScale.x * boxCollider.size.x * 0.5f), UnityEngine.Random.Range(area.transform.position.y - area.transform.localScale.y * boxCollider.size.y * 0.5f, area.transform.position.y + area.transform.localScale.y * boxCollider.size.y * 0.5f)); }while (Vector2.Distance(randomPosition, target.transform.position) < closeDistance); navAgent.CalculatePath(randomPosition, path); Debug.Log("Iteration #" + numIterations + ": " + path.corners.Length + "corners"); numIterations++; }while (numIterations < 10 && CheckIfPathIntersectsPlayer(path)); if (numIterations >= 10) { navAgent.SetDestination(navAgent.transform.position); } navAgent.SetPath(path); return(randomPosition); } else { SphereCollider sphereCollider = area != null?area.GetComponent <SphereCollider>() : null; if (sphereCollider != null) { float distance = UnityEngine.Random.Range(-sphereCollider.radius, area.transform.localScale.x * sphereCollider.radius); float angle = UnityEngine.Random.Range(0, 2 * Mathf.PI); return(new Vector3(area.transform.position.x + distance * Mathf.Cos(angle), area.transform.position.y, area.transform.position.z + distance * Mathf.Sin(angle))); } else { return(gameObject.transform.position + new Vector3(UnityEngine.Random.Range(-5f, 5f), 0, UnityEngine.Random.Range(-5f, 5f))); } } }
static public int SetPath(IntPtr l) { try { UnityEngine.AI.NavMeshAgent self = (UnityEngine.AI.NavMeshAgent)checkSelf(l); UnityEngine.AI.NavMeshPath a1; checkType(l, 2, out a1); var ret = self.SetPath(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
static int SetPath(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); UnityEngine.AI.NavMeshAgent obj = (UnityEngine.AI.NavMeshAgent)ToLua.CheckObject(L, 1, typeof(UnityEngine.AI.NavMeshAgent)); UnityEngine.AI.NavMeshPath arg0 = (UnityEngine.AI.NavMeshPath)ToLua.CheckObject(L, 2, typeof(UnityEngine.AI.NavMeshPath)); bool o = obj.SetPath(arg0); LuaDLL.lua_pushboolean(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public override void Execute() { // create a Raycast and set it to the mouses cursor position in game Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, lm)) { path = new UnityEngine.AI.NavMeshPath(); if (agent.CalculatePath(hit.point, path)) { agent.SetPath(path); } else { path = null; } } }
// Update is called once per frame void Update() { if (target == null && !gettingTarget) { StartCoroutine(AcquireTarget()); } if (target != null) { if (myAgent.CalculatePath(target.transform.position, nextPath)) { if (nextPath.status == UnityEngine.AI.NavMeshPathStatus.PathPartial) { target = null; //Debug.Log("Partially blocked - Not reachable"); } else { myAgent.SetPath(nextPath); } } else { target = null; //Debug.Log("Not reachable"); } } if (inRange) { if (isBuilder) { //do rebuild RebuildNode(); } if (!isBuilder) { //do attack AttackTarget(); //Debug.Log("In attack range of player"); } } }
public void MoveTo(Vector3 location) { if (!location.Equals(Vector3.zero)) { NavMeshPath path = new NavMeshPath(); agent.CalculatePath(location, path); agent.SetPath(path); pathArray = path.corners; if (path.corners.Length > 2) { bMoving = true; currentPoint = 1; currentVector = path.corners[currentPoint]; } else { currentVector = path.corners[1]; } RpcMoveTo(currentVector); } }