IEnumerator FSM() { while (alive) { switch (state) { case State.PATROL: Patrol(); break; case State.CHASE: Chase(); break; } if (agent.isOnOffMeshLink) { character.Move(Vector3.zero, false, true); if (method == OffMeshLinkMoveMethod.NormalSpeed) { yield return(StartCoroutine(NormalSpeed(agent))); } else if (method == OffMeshLinkMoveMethod.Parabola) { yield return(StartCoroutine(Parabola(agent, jumpHeight, jumpDuration))); } agent.CompleteOffMeshLink(); } yield return(null); } }
void DoCompleteOffMeshLink() { if (_agent == null) { return; } _agent.CompleteOffMeshLink(); }
IEnumerator NormalSpeed(UnityEngine.AI.NavMeshAgent agent) { UnityEngine.AI.OffMeshLinkData data = agent.currentOffMeshLinkData; Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset; while (agent.transform.position != endPos) { //Debug.DrawLine(transform.position, endPos, Color.red); transform.position = Vector3.MoveTowards(transform.position, endPos, 6f * Time.deltaTime); agent.enabled = false; yield return(new WaitForEndOfFrame()); } if (agent.enabled && !agent.autoTraverseOffMeshLink) { agent.CompleteOffMeshLink(); } agent.enabled = true; }
IEnumerator JumpMod() { while (alive) { if (agent.isOnOffMeshLink) { character.Move(Vector3.zero, false, true); if (method == OffMeshLinkMoveMethod.NormalSpeed) { yield return(StartCoroutine(NormalSpeed(agent))); } else if (method == OffMeshLinkMoveMethod.Parabola) { yield return(StartCoroutine(Parabola(agent, jumpHeight, jumpDuration))); } agent.CompleteOffMeshLink(); } yield return(null); } }
IEnumerator Parabola(UnityEngine.AI.NavMeshAgent agent, float height, float duration) { UnityEngine.AI.OffMeshLinkData data = agent.currentOffMeshLinkData; Vector3 startPos = agent.transform.position; Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset; float normalizedTime = 0.0f; while (normalizedTime < 1.0f) { float yOffset = height * 4.0f * (normalizedTime - normalizedTime * normalizedTime); agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up; normalizedTime += Time.deltaTime / duration; yield return(null); } if (agent.enabled && !agent.autoTraverseOffMeshLink) { agent.CompleteOffMeshLink(); } }
//Instantly move the object to the end of the link public virtual void CompleteOffMeshLink() { agent.CompleteOffMeshLink(); }
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); } }