void GoToWaypoint() { UnityEngine.AI.NavMeshPath path = new UnityEngine.AI.NavMeshPath(); Vector3 newLocation = Vector3.zero; while (path.status == UnityEngine.AI.NavMeshPathStatus.PathPartial || path.status == UnityEngine.AI.NavMeshPathStatus.PathInvalid) { Vector3 ran = Random.insideUnitSphere * WanderRadius; ran.y = _startingPos.y; newLocation = _startingPos + ran; _navMeshAgent.CalculatePath(newLocation, path); } _navMeshAgent.SetDestination(newLocation); _hasReachedDestination = false; }
/// <summary>Initialization Method of MoveToPosition.</summary> /// <remarks>Check if there is a NavMeshAgent to assign a default one and assign the destination to the NavMeshAgent the given position.</remarks> public override void OnStart() { navAgent = gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>(); if (navAgent == null) { Debug.LogWarning("The " + gameObject.name + " game object does not have a Nav Mesh Agent component to navigate. One with default values has been added", gameObject); navAgent = gameObject.AddComponent <UnityEngine.AI.NavMeshAgent>(); } navAgent.SetDestination(target); #if UNITY_5_6_OR_NEWER navAgent.isStopped = false; #else navAgent.Resume(); #endif }
private void Update() { RotateView(); nav.SetDestination(nextScene.transform.position); if (CrossPlatformInputManager.GetButtonDown("Jump") && !m_Jump) { m_Jump = true; } print(Vector3.Distance(player.transform.position, nextScene.transform.position)); if (Vector3.Distance(player.transform.position, nextScene.transform.position) < 10f) { SceneManager.LoadScene("VR"); } }
protected void FollowPlayer() { // If the enemy and the player have health left... if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0 && !isStunned) { // ... set the destination of the nav mesh agent to the player. nav.enabled = true; nav.SetDestination(player.position); } // Otherwise... else { // ... disable the nav mesh agent. nav.enabled = false; } }
void PathToPlayer() { // If the enemy and the player have health left... if (enemyHealth.currentHealth > 0 && HealthManager.instance.allPlayersAlive && hitsUntilFrozen > 0) { // ... set the destination of the nav mesh agent to the player. nav.SetDestination(target.position); //Debug.Log(gameObject.name + " moving towards " + target.name); } // Otherwise... else { // ... disable the nav mesh agent. nav.enabled = false; } }
public override void OnStart() { destination_out = destination; ammo_out = ammo; if (ammo_out != 0) { Debug.Log("Tank " + gameObject.name + "Still has ammo! Ammo: " + ammo_out); return; } agent = gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>(); agent.SetDestination(base_waypoint.position); // Will set the destination to the waypoint with the "destination" index. agent.isStopped = false; }
void CheckIfIShouldFlee() { if (isFleeing == true) { if (fleeTarget != null && !enemyMaster.isOnRoute && !enemyMaster.isNavPaused) { float distanceOf = Vector3.Distance(myTransform.position, fleeTarget.position); if (FleeTarget(out runPosition) && distanceOf < fleeRange) { myNavMeshAgent.SetDestination(runPosition); enemyMaster.CallEventEnemyWalking(); enemyMaster.isOnRoute = true; } } } }
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. //seting this for multi distination nav.SetDestination(player[idAttack].transform.position); //nav.SetDestination(player.position); } // Otherwise... else { // ... disable the nav mesh agent. nav.enabled = false; } }
private void GoToDrop() { Vector3 distance = Vector3.zero; distance.x = Mathf.Abs(objective.transform.position.x - RedTank.transform.position.x); distance.z = Mathf.Abs(objective.transform.position.z - RedTank.transform.position.z); if (distance.x > 1 && distance.z > 1) { navAgent.SetDestination(objective.transform.position); } else { arrived = true; } }
public override Status Update() { if (!memory) { return(Status.Error); } ICover nearstCover = NearstCover(); if (nearstCover == null) { return(Status.Failure); } agent.SetDestination(nearstCover.pos); agent.Resume(); return(Status.Running); }
public override Node.Task OnUpdate() { // prevent if target == null if (pursueGameObject == null) { return(Task.Failure); } _agent.SetDestination(Target()); if (HasArrived()) { hasFinished = true; return(Task.Success); } return(Task.Running); }
IEnumerator UpdatePath() { while (target != null) { if (currentState == EnemyState.Chasing) { Vector3 directionToTarget = (target.position - transform.position).normalized; // Set pathfinding position so that we don't go 'inside' the target. We also add half the attackDistance to prevent 'crowding' the target. Vector3 targetPosition = target.position - directionToTarget * (myCollisionRadius + targetCollisionRadius + attackDistanceThreshold / 2); if (!dead) { pathfinder.SetDestination(targetPosition); } } yield return(new WaitForSeconds(refreshRate)); } }
void Move() { // Create a ray from the mouse cursor on screen in the direction of the camera. Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition); // Create a RaycastHit variable to store information about what was hit by the ray. RaycastHit floorHit; // Perform the raycast and if it hits something on the floor layer... if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask)) { nav.enabled = true; nav.SetDestination(floorHit.point); hitpoint = floorHit.point; Target = null; shooting.target = false; } }
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. if (System.Environment.MachineName == MasterTrackingData.HeadNodeMachineName) { nav.SetDestination(player.position); } //} // Otherwise... //else //{ // ... disable the nav mesh agent. // nav.enabled = false; //} }
// Update is called once per frame protected void Update() { if (waypoints == null) { return; } if (lastRequest == null) { lastRequest = GetNewPosition(); navAgent.SetDestination(lastRequest.Value); // Debug.Log("Moving to: " + lastRequest.Value); } if (!navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance) { lastRequest = null; } }
public override Status Update() { BaseAIParameters param = self.GetComponent <BaseAIParameters>(); if (!param || !param.destinationEnable) { return(Status.Failure); } if (Vector3.Distance(self.transform.position, param.destination) < 2) { return(Status.Success); } UnityEngine.AI.NavMeshAgent agent = self.GetComponent <UnityEngine.AI.NavMeshAgent>(); agent.SetDestination(param.destination); agent.Resume(); return(Status.Running); }
public virtual void handleAttackPlayer() { if (m_target && m_attackTime < 0 && m_hitTime < 0) { Vector3 vec = m_target.transform.position - transform.position; float d0 = vec.magnitude; if (m_target.transform.position.x > transform.position.x) { transform.rotation = Quaternion.AngleAxis(90, Vector3.up); } else { transform.rotation = Quaternion.AngleAxis(-90, Vector3.up); } if (d0 > attackRange) { if (m_attackTime < 0 && m_hitTime < 0) { m_animator.SetBool("Walk", true); m_animator.SetBool("Attack", false); if (m_agent && m_agent.isOnNavMesh) { m_agent.Resume(); m_agent.SetDestination(m_target.transform.position); } else { // putOnNavMesh(m_target.transform.position); // Debug.Log ("Put On Path"); } } } else { handleAttack(); } } }
public override Status Update() { //BaseAIParameters param = self.GetComponent<BaseAIParameters>(); //if (!param || !param.destinationEnable) //{ // return Status.Failure; //} //if (Vector3.Distance(self.transform.position, param.destination) < param.endSeekingDistance) //{ // return Status.Success; //} Memory memory = self.GetComponent <Memory>(); if (!memory) { return(Status.Failure); } ITarget[] targets = memory.AllTargets(); bool hasTarget = targets.Length > 0; if (hasTarget) { ITarget target = targets[0]; IMemorable memorable = target as IMemorable; IMemorableItem memItem = memory.Find(memorable); Vector3 destination = memItem.lastOccurPosition; UnityEngine.AI.NavMeshAgent agent = self.GetComponent <UnityEngine.AI.NavMeshAgent>(); agent.SetDestination(destination); agent.Resume(); return(Status.Running); } else { UnityEngine.AI.NavMeshAgent agent = self.GetComponent <UnityEngine.AI.NavMeshAgent>(); agent.Stop(); return(Status.Failure); } }
public override TaskStatus OnUpdate() { if (ammo_out != 0) { return(TaskStatus.COMPLETED); } if (!agent.pathPending && agent.remainingDistance < 0.5f) { ammo_out = 3; destination_out = 0; return(TaskStatus.COMPLETED); } else if (agent.destination != base_waypoint.transform.position) { agent.SetDestination(base_waypoint.position); } return(TaskStatus.RUNNING); }
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); Vector3 distance = new Vector3(1, 0, 1); if (gameObject.transform.position - player.position == distance) { Debug.Log("shoot"); } } // Otherwise... else { // ... disable the nav mesh agent. nav.enabled = false; } }
public override Status Update() { // Get the renderer if (m_GOAgent == null || m_GOAgent.gameObject != gameObject.Value) { m_GOAgent = gameObject.Value != null?gameObject.Value.GetComponent <UnityEngine.AI.NavMeshAgent>() : null; } // Validate members? if (m_GOAgent == null || target.Value == null) { return(Status.Error); } if (target.transform.hasChanged) { m_GOAgent.SetDestination(target.transform.position); } return(Status.Running); }
public override Node.Task OnUpdate() { if (HasArrived()) { if (waypointReachedTime == -1) { waypointReachedTime = Time.time; } // wait the required duration before switching waypoints. if (waypointReachedTime + waypointPauseDuration <= Time.time) { if (randomPatrol) { if (waypoints.Count == 1) { waypointIndex = 0; } else { // prevent the same waypoint from being selected var newWaypointIndex = waypointIndex; while (newWaypointIndex == waypointIndex) { newWaypointIndex = Random.Range(0, waypoints.Count); } waypointIndex = newWaypointIndex; } } else { waypointIndex = (waypointIndex + 1) % waypoints.Count; } _agent.SetDestination(Target()); waypointReachedTime = -1; } } return(Task.Running); }
public override void OnStart() { navAgent = gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>(); if (navAgent == null) { Debug.LogWarning("The " + gameObject.name + " game object does not have a Nav Mesh Agent component to navigate. One with default values has been added", gameObject); navAgent = gameObject.AddComponent <UnityEngine.AI.NavMeshAgent>(); } Transform waypoint = GetRandomWaypoint(); if (waypoint == null) { Debug.LogWarning("No waypoints have been given for " + gameObject.name + " to navigate to.", gameObject); } else { navAgent.SetDestination(waypoint.position); } navAgent.isStopped = false; }
/// <summary> /// This method updates the NavMesh Agent thus allowing the enemy to move. /// If the player is within the enemy's range then the enemy will move towards /// the player. Otherwhise he will stop. If the player is within melee range /// then the enemy will always face the player. /// </summary> void Movement() { distance = Vector3.Distance(player.position, transform.position); if (Physics.Raycast(collider.transform.TransformPoint(collider.center), playerDir, out hitInfo, viewLenght) && hitInfo.transform.tag == "Player" && distance > attackDistance) { Debug.Log("Player is in range and the enemy is following him"); nav.SetDestination(player.position); nav.Resume(); } else if (distance < attackDistance) { Debug.Log("The enemy is close enough to attack"); nav.Stop(true); transform.LookAt(player.transform); } else if (distance > viewDistance) { nav.Stop(true); } }
private void SetCharacterPosition() { /* * Move the character, unless they are spot-turning. */ if (_char != null && !_char.IsTurningBeforeWalking()) { /* * We could just set the destination as _char.GetTargetPosition(), but this function will return the character's position if they * are not on a path. This is normally fine, but the path is also removed once the character begins decelerating. * Therefore, we record the "last known" targetPosition for as long as the character is on a Path. * Note: If the character overshoots and starts 'sliding' backward, try increasing the NavMeshAgent's stopping distance. */ if (_char.GetPath() || _char.charState == CharState.Idle) { targetPosition = _char.GetTargetPosition(); } /** * Stop the character from running if they are closer than the "Stopping distance" to from their target. */ if (_char.isRunning && Vector3.Distance(targetPosition, transform.position) < navMeshAgent.stoppingDistance) { if (_char.WillStopAtNextNode()) { _char.isRunning = false; } } /* * Scale the speed if we are running. Note that the original speed is recorded in Start(), so you will need to replay the scene * to see changes made to the NavMeshAgent's "speed" variable take effect. */ navMeshAgent.speed = (_char.isRunning) ? (originalSpeed * runSpeedFactor) : originalSpeed; navMeshAgent.SetDestination(targetPosition); } }
private void SetDestination(Vector3 position) { if (agents.Count < 1) { return; } master.SetDestination(position); int currentInRow = 1; int currentInColumn = 0; for (int i = 1; i < agents.Count; i++) { UnityEngine.AI.NavMeshAgent mAgent = agents[i]; mAgent.speed = master.speed * 1.5f; mAgent.SetDestination(master.transform.position - master.transform.right * currentInRow * 2 - master.transform.forward * currentInColumn * 2); currentInRow++; if (currentInRow == maxInRow) { currentInRow = 0; currentInColumn++; } } }
void Start() { _agent = GetComponentInChildren <UnityEngine.AI.NavMeshAgent>(); _pawn = GetComponentInParent <Pawn>(); _targetReached = true; if (_randomPosition) { _target = new GameObject().transform; Random.InitState((int)System.DateTime.Now.Ticks); } else { if (_target) { _oldTargetPos = _target.position; _agent.SetDestination(_target.position); } } _agent.updateRotation = false; _agent.updatePosition = false; }
void Update() { var closestPlayer = FindClosestPlayer(); if (closestPlayer == null) { nav.enabled = false; } else { nav.enabled = true; } var playerHealth = closestPlayer.GetComponent <PlayerHealth>(); if (enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0) { nav.SetDestination(closestPlayer.transform.position); } else { nav.enabled = false; } }
public override void OnStart() { navMeshAgent.SetDestination(target.Value.position); }
private IEnumerator StateGen() { int node = 0; NodeVal ret = NodeVal.Error; _jmp = -1; comp_Animator = GetComponent <Animator>(); comp_navMeshAgent = GetComponent <UnityEngine.AI.NavMeshAgent>(); float f1 = 0.0f; int n0c = -1; int n1_1i = 0; NodeVal n0_1r = NodeVal.Invalid; NodeVal r1 = NodeVal.Invalid; NodeVal r2 = NodeVal.Invalid; valid = valid && (comp_Animator != null); valid = valid && (comp_navMeshAgent != null); if (!valid) { throw new BehaviourTreeException("Behaviour tree will not run. The GameObject has missing required components."); } do { switch (node) { case 0: #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(1, _liveHandle, NodeVal.Running, false, true); } #endif n0c = 1; node = 1; n0_1r = NodeVal.Success; break; case 12: if (_jmp >= 0) { node = _jmp; _jmp = -1; continue; } n0_1r = (NodeVal)System.Math.Max((int)n0_1r, (int)ret); if (ret == NodeVal.Success && n0c < 3) { node = ++n0c; } else { n0c = 1; node = 0; ret = n0_1r; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(1, _liveHandle, ret, true, false); } #endif yield return(null); } break; case 1: #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(8, _liveHandle, NodeVal.Running, false, false); } #endif do { node = UnityEngine.Random.Range(4, 10); } while (n1_1i == node); n1_1i = node; break; case 13: node = 12; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(8, _liveHandle, ret, false, false); } #endif break; case 2: if (!comp_navMeshAgent.SetDestination((this.Target).position)) { node = 12; ret = NodeVal.Fail; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(9, _liveHandle, ret, false, false); } #endif break; } do { ret = (comp_navMeshAgent.pathPending || comp_navMeshAgent.remainingDistance > comp_navMeshAgent.stoppingDistance || comp_navMeshAgent.velocity.sqrMagnitude != 0f)? NodeVal.Running : NodeVal.Success; if (ret == NodeVal.Running) { #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(9, _liveHandle, NodeVal.Running, true, false); } #endif yield return(null); } else { node = 12; break; } } while (true); #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(9, _liveHandle, ret, false, false); } #endif break; case 3: f1 = Time.time; r1 = NodeVal.Running; r2 = NodeVal.Running; ret = NodeVal.Success; do { if (r1 == NodeVal.Running) { /* Animation */ comp_Animator.SetTrigger("Tip"); r1 = NodeVal.Success; } ret = r1; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(11, _liveHandle, r1, false, false); } #endif if (r2 == NodeVal.Running) { /* Wait */ r2 = ((Time.time - f1) > 2f)? NodeVal.Success : NodeVal.Running; } ret = (NodeVal)System.Math.Max((int)r2, (int)ret); #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(12, _liveHandle, r2, false, false); } #endif if (ret == NodeVal.Running) { #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(10, _liveHandle, NodeVal.Running, true, false); } #endif yield return(null); } else { node = 12; break; } } while (true); #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(10, _liveHandle, ret, false, false); } #endif break; case 4: this.Target = this.Target1; ret = NodeVal.Success; node = 13; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(7, _liveHandle, ret, false, false); } #endif break; case 5: this.Target = this.Target2; ret = NodeVal.Success; node = 13; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(6, _liveHandle, ret, false, false); } #endif break; case 6: this.Target = this.Target3; ret = NodeVal.Success; node = 13; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(5, _liveHandle, ret, false, false); } #endif break; case 7: this.Target = this.Target4; ret = NodeVal.Success; node = 13; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(2, _liveHandle, ret, false, false); } #endif break; case 8: this.Target = this.Target5; ret = NodeVal.Success; node = 13; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(3, _liveHandle, ret, false, false); } #endif break; case 9: this.Target = this.Target6; ret = NodeVal.Success; node = 13; #if UNITY_EDITOR if (_live != null) { _live = _live.SetLiveValue(4, _liveHandle, ret, false, false); } #endif break; } } while (node >= 0); yield return(null); }