Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. using UnityEngine;
  5. using Pathfinding;
  6.  
  7. public class Ghost : MonoBehaviour {
  8.     public Rigidbody2D body2D;
  9.     // The point to move to
  10.     public Vector3 targetPosition;
  11.     private Seeker seeker;
  12.     // The calculated path
  13.     public Path path;
  14.     // The max distance from the AI to a waypoint for it to continue to the next waypoint
  15.     public float nextWaypointDistance = 3;
  16.     // The waypoint we are currently moving towards
  17.     private int currentWaypoint = 0;
  18.     // How often to recalculate the path (in seconds)
  19.     public float repathRate = 2f;
  20.     private float lastRepath = -9999;
  21.     public void Start () {
  22.         seeker = GetComponent<Seeker>();
  23.  
  24.         body2D = GetComponent<Rigidbody2D> ();
  25.         body2D.freezeRotation = true;
  26.  
  27.         targetPosition = GameObject.FindGameObjectWithTag ("Target").transform.position;
  28.     }
  29.     public void OnPathComplete (Path p) {
  30.         Debug.Log("A path was calculated. Did it fail with an error? " + p.error);
  31.         if (!p.error) {
  32.             path = p;
  33.             // Reset the waypoint counter so that we start to move towards the first point in the path
  34.             currentWaypoint = 0;
  35.         }
  36.     }
  37.     public void FixedUpdate () {
  38.         if (Time.time - lastRepath > repathRate && seeker.IsDone()) {
  39.             lastRepath = Time.time+ Random.value*repathRate*0.5f;
  40.             // Start a new path to the targetPosition, call the the OnPathComplete function
  41.             // when the path has been calculated (which may take a few frames depending on the complexity)
  42.             Debug.Log("Seeker update");
  43.             seeker.StartPath(transform.position, targetPosition, OnPathComplete);
  44.         }
  45.         if (path == null) {
  46.             // We have no path to follow yet, so don't do anything
  47.             return;
  48.         }
  49.         if (currentWaypoint > path.vectorPath.Count) return;
  50.         if (currentWaypoint == path.vectorPath.Count) {
  51.             Debug.Log("End Of Path Reached");
  52.             currentWaypoint++;
  53.             return;
  54.         }
  55.         // Direction to the next waypoint
  56.         Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
  57.         body2D.velocity = dir;
  58.         // The commented line is equivalent to the one below, but the one that is used
  59.         // is slightly faster since it does not have to calculate a square root
  60.         //if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
  61.         if ((transform.position-path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance*nextWaypointDistance) {
  62.             Debug.Log("currentWayPoint: " + currentWaypoint);
  63.             currentWaypoint++;
  64.             return;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement