123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- #pragma warning disable 618
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace Pathfinding.Legacy {
- using Pathfinding;
- using Pathfinding.RVO;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [RequireComponent(typeof(Seeker))]
- [AddComponentMenu("Pathfinding/Legacy/AI/Legacy AIPath (3D)")]
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_legacy_1_1_legacy_a_i_path.php")]
- public class LegacyAIPath : AIPath {
-
-
-
-
- public float forwardLook = 1;
-
-
-
-
-
-
-
-
- public bool closestOnPathCheck = true;
- protected float minMoveScale = 0.05F;
-
- protected int currentWaypointIndex = 0;
- protected Vector3 lastFoundWaypointPosition;
- protected float lastFoundWaypointTime = -9999;
- protected override void Awake () {
- base.Awake();
- if (rvoController != null) {
- if (rvoController is LegacyRVOController) (rvoController as LegacyRVOController).enableRotation = false;
- else Debug.LogError("The LegacyAIPath component only works with the legacy RVOController, not the latest one. Please upgrade this component", this);
- }
- }
-
-
-
-
-
- protected override void OnPathComplete (Path _p) {
- ABPath p = _p as ABPath;
- if (p == null) throw new System.Exception("This function only handles ABPaths, do not use special path types");
- waitingForPathCalculation = false;
-
- p.Claim(this);
-
-
- if (p.error) {
- p.Release(this);
- return;
- }
-
- if (path != null) path.Release(this);
-
- path = p;
-
- currentWaypointIndex = 0;
- reachedEndOfPath = false;
-
-
-
-
- if (closestOnPathCheck) {
-
-
-
-
-
- Vector3 p1 = Time.time - lastFoundWaypointTime < 0.3f ? lastFoundWaypointPosition : p.originalStartPoint;
- Vector3 p2 = GetFeetPosition();
- Vector3 dir = p2-p1;
- float magn = dir.magnitude;
- dir /= magn;
- int steps = (int)(magn/pickNextWaypointDist);
- #if ASTARDEBUG
- Debug.DrawLine(p1, p2, Color.red, 1);
- #endif
- for (int i = 0; i <= steps; i++) {
- CalculateVelocity(p1);
- p1 += dir;
- }
- }
- }
- protected override void Update () {
- if (!canMove) { return; }
- Vector3 dir = CalculateVelocity(GetFeetPosition());
-
- RotateTowards(targetDirection);
- if (rvoController != null) {
- rvoController.Move(dir);
- } else
- if (controller != null) {
- controller.SimpleMove(dir);
- } else if (rigid != null) {
- rigid.AddForce(dir);
- } else {
- tr.Translate(dir*Time.deltaTime, Space.World);
- }
- }
-
-
-
-
- protected new Vector3 targetDirection;
- protected float XZSqrMagnitude (Vector3 a, Vector3 b) {
- float dx = b.x-a.x;
- float dz = b.z-a.z;
- return dx*dx + dz*dz;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected new Vector3 CalculateVelocity (Vector3 currentPosition) {
- if (path == null || path.vectorPath == null || path.vectorPath.Count == 0) return Vector3.zero;
- List<Vector3> vPath = path.vectorPath;
- if (vPath.Count == 1) {
- vPath.Insert(0, currentPosition);
- }
- if (currentWaypointIndex >= vPath.Count) { currentWaypointIndex = vPath.Count-1; }
- if (currentWaypointIndex <= 1) currentWaypointIndex = 1;
- while (true) {
- if (currentWaypointIndex < vPath.Count-1) {
-
- float dist = XZSqrMagnitude(vPath[currentWaypointIndex], currentPosition);
-
- if (dist < pickNextWaypointDist*pickNextWaypointDist) {
- lastFoundWaypointPosition = currentPosition;
- lastFoundWaypointTime = Time.time;
- currentWaypointIndex++;
- } else {
- break;
- }
- } else {
- break;
- }
- }
- Vector3 dir = vPath[currentWaypointIndex] - vPath[currentWaypointIndex-1];
- Vector3 targetPosition = CalculateTargetPoint(currentPosition, vPath[currentWaypointIndex-1], vPath[currentWaypointIndex]);
- dir = targetPosition-currentPosition;
- dir.y = 0;
- float targetDist = dir.magnitude;
- float slowdown = Mathf.Clamp01(targetDist / slowdownDistance);
- this.targetDirection = dir;
- if (currentWaypointIndex == vPath.Count-1 && targetDist <= endReachedDistance) {
- if (!reachedEndOfPath) { reachedEndOfPath = true; OnTargetReached(); }
-
- return Vector3.zero;
- }
- Vector3 forward = tr.forward;
- float dot = Vector3.Dot(dir.normalized, forward);
- float sp = maxSpeed * Mathf.Max(dot, minMoveScale) * slowdown;
- #if ASTARDEBUG
- Debug.DrawLine(vPath[currentWaypointIndex-1], vPath[currentWaypointIndex], Color.black);
- Debug.DrawLine(GetFeetPosition(), targetPosition, Color.red);
- Debug.DrawRay(targetPosition, Vector3.up, Color.red);
- Debug.DrawRay(GetFeetPosition(), dir, Color.yellow);
- Debug.DrawRay(GetFeetPosition(), forward*sp, Color.cyan);
- #endif
- if (Time.deltaTime > 0) {
- sp = Mathf.Clamp(sp, 0, targetDist/(Time.deltaTime*2));
- }
- return forward*sp;
- }
-
-
-
-
-
- protected void RotateTowards (Vector3 dir) {
- if (dir == Vector3.zero) return;
- Quaternion rot = tr.rotation;
- Quaternion toTarget = Quaternion.LookRotation(dir);
- rot = Quaternion.Slerp(rot, toTarget, turningSpeed*Time.deltaTime);
- Vector3 euler = rot.eulerAngles;
- euler.z = 0;
- euler.x = 0;
- rot = Quaternion.Euler(euler);
- tr.rotation = rot;
- }
-
-
-
-
-
-
-
-
-
- protected Vector3 CalculateTargetPoint (Vector3 p, Vector3 a, Vector3 b) {
- a.y = p.y;
- b.y = p.y;
- float magn = (a-b).magnitude;
- if (magn == 0) return a;
- float closest = Mathf.Clamp01(VectorMath.ClosestPointOnLineFactor(a, b, p));
- Vector3 point = (b-a)*closest + a;
- float distance = (point-p).magnitude;
- float lookAhead = Mathf.Clamp(forwardLook - distance, 0.0F, forwardLook);
- float offset = lookAhead / magn;
- offset = Mathf.Clamp(offset+closest, 0.0F, 1.0F);
- return (b-a)*offset + a;
- }
- }
- }
|