//constructs the tween and starts movement private IEnumerator Move() { //enable agent updates agent.Resume(); agent.updateRotation = updateRotation; //if move to path is enabled, //set an additional destination to the first waypoint if (moveToPath) { agent.SetDestination(waypoints[currentPoint].position); yield return(StartCoroutine(WaitForDestination())); moveToPath = false; } else { //set the transform's position to the first waypoint agent.Warp(waypoints[currentPoint].position); } //we're now at the first waypoint position, so directly call the next waypoint. //on looptype random we have to initialize a random order of waypoints first. if (loopType == LoopType.random) { StartCoroutine(ReachedEnd()); } else { StartCoroutine(NextWaypoint()); } }
public void ImmediateMove(Vector3 pos, Quaternion dir) { if (m_agent) { m_agent.enabled = false; } InternalImmediateStop(); transform.position = pos; transform.rotation = dir; Debug.Log(transform.position); m_mouseLook.Init(transform, Camera.transform); if (m_agent) { m_agent.enabled = true; if (m_agent.isActiveAndEnabled) { m_agent.updatePosition = false; m_agent.Warp(pos); m_agent.updatePosition = true; } } MovePlayer(Vector3.zero); }
void FinishAction() { if (currentNavAgent != null) { switch (currentNavAction) { case NavAgentActions.SetDestination: if (!preCalcPaths) { currentNavAgent.SetDestination(currentNavDest); } currentNavAgent.speed = currentNavSpeed; //Debug.Log("Set Nav dest"); break; case NavAgentActions.Stop: currentNavAgent.Stop(); break; case NavAgentActions.Warp: currentNavAgent.Warp(currentNavDest); break; } } else { Debug.LogWarning("No current Nav Agent was found"); } }
//object reached the end of its path private IEnumerator ReachedEnd() { //each looptype has specific properties switch (loopType) { //LoopType.none means there will be no repeat, //so we just execute the final event case LoopType.none: OnWaypointChange(waypoints.Length - 1); yield break; //in a loop we set our position indicator back to zero, //also executing last event case LoopType.loop: OnWaypointChange(waypoints.Length - 1); //additional option: if the path was closed, we move our object //from the last to the first waypoint instead of just "appearing" there if (closeLoop) { agent.SetDestination(waypoints[0].position); yield return(StartCoroutine(WaitForDestination())); } else { agent.Warp(waypoints[0].position); } currentPoint = 0; break; //on LoopType.pingPong, we have to invert currentPoint updates case LoopType.pingPong: repeat = !repeat; break; //on LoopType.random, we calculate a random order between all waypoints //and loop through them, for this case we use the Fisher-Yates algorithm case LoopType.random: RandomizeWaypoints(); break; } //start moving to the next iteration StartCoroutine(NextWaypoint()); }
private void OnTeleport() { /* * This function is called by the Char script whenever the character has been teleported. */ targetPosition = _char.GetTargetPosition(); navMeshAgent.Warp(transform.position); }
public override TaskStatus OnUpdate() { if (navMeshAgent == null) { Debug.LogWarning("NavMeshAgent is null"); return(TaskStatus.Failure); } navMeshAgent.Warp(newPosition.Value); return(TaskStatus.Success); }
void DoWarp() { if (_agent == null) { return; } bool ok = _agent.Warp(newPosition.Value); success.Value = ok; if (ok) { Fsm.Event(successEvent); } else { Fsm.Event(failureEvent); } }
public void Restart() { // When the game starts the character should be able to move and the collider should be on but the player is not dead. m_AiCharacter.enabled = true; m_Collider.enabled = true; m_IsDying = false; // Move the character back to the start position. m_Agent.Warp(m_OriginPosition); // Make sure the character is already at the position that is trying to be reached. m_AiCharacter.SetTarget(transform.position); // Reset the animator. m_Animator.SetTrigger(m_HashResetPara); // The game is not over. m_IsGameOver = false; }
public void SetPosition(Vector3 pos, Quaternion rot) { _navMesh.Warp(pos); //_transform.position = pos; _transform.rotation = rot; }
void FixedUpdate() { if (_randomPosition && _targetReached) { Vector3 randomDirection = Random.insideUnitSphere * _randomWalkRadius; randomDirection += transform.position; UnityEngine.AI.NavMeshHit hit; UnityEngine.AI.NavMesh.SamplePosition(randomDirection, out hit, _randomWalkRadius, 1); _target.position = hit.position; _oldTargetPos = hit.position; _agent.SetDestination(_target.position); _targetReached = false; } if (_target != null) { // recalculate path if needed if (Vector3.Distance(_target.position, _oldTargetPos) > 1f) { _oldTargetPos = _target.position; _agent.SetDestination(_target.position); } // custom off mesh traversal code if (_agent.isOnOffMeshLink) { Vector3 offMeshDir = (_agent.currentOffMeshLinkData.endPos - _agent.currentOffMeshLinkData.startPos).normalized; Vector3 endPos = _agent.currentOffMeshLinkData.endPos + Vector3.up * _agent.baseOffset + offMeshDir * 0.05f; Vector3 offMeshDist = transform.position - endPos; offMeshDist.y = 0; if (Mathf.Abs(offMeshDist.sqrMagnitude) < 0.1f) { _agent.CompleteOffMeshLink(); } else { _pawn.Move((endPos - transform.position)); } } else { // // use the values to move the character _pawn.Move(_agent.desiredVelocity.normalized); _agent.nextPosition = transform.position; } // warp the agent if needed (sometimes the agent thinks its on a highter level, but the character is still on ground) if (_pawn.IsOnGround && Mathf.Abs(_agent.nextPosition.y - transform.position.y) > 0.5f) { _agent.Warp(transform.position); _agent.SetDestination(_target.position); } if ((_agent.desiredVelocity.normalized == Vector3.zero) || (_agent.remainingDistance <= _agent.stoppingDistance + 0.01)) { _targetReached = true; } } else { // We still need to call the character's move function, but we send zeroed input as the move param. _pawn.Move(Vector3.zero); } }