Advertisement
Guest User

Mecanim with A* Pathfinding Project

a guest
Jan 20th, 2014
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1.     using UnityEngine;
  2.     using System.Collections;
  3.     using Pathfinding;
  4.      
  5.     public class AgentAStar : MonoBehaviour {
  6.      
  7.         public GameObject           particle;
  8.         protected Seeker            seeker;
  9.         protected Animator          animator;
  10.      
  11.         protected Locomotion locomotion;
  12.         protected Object particleClone;
  13.      
  14.         // The calculated path
  15.         public Path path;
  16.      
  17.         // The AI's speed per second
  18.         public float speed = 1;
  19.      
  20.         // The max distance from the AI to a waypoint for it to continue to the next waypoint
  21.         public float nextWaypointDistance = 3;
  22.      
  23.         // The waypoint we are currently moving towards
  24.         private int currentWaypoint = 0;
  25.      
  26.         // Use this for initialization
  27.         void Start ()
  28.         {
  29.             seeker = GetComponent<Seeker>();
  30.      
  31.             animator = GetComponent<Animator>();
  32.             locomotion = new Locomotion(animator);
  33.      
  34.             particleClone = null;
  35.         }
  36.      
  37.         protected void SetDestination()
  38.         {
  39.             // Construct a ray from the current mouse coordinates
  40.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  41.             RaycastHit hit = new RaycastHit();
  42.             if (Physics.Raycast(ray, out hit))
  43.             {
  44.                 if (particleClone != null)
  45.                 {
  46.                     GameObject.Destroy(particleClone);
  47.                     particleClone = null;
  48.                 }
  49.      
  50.                 // Create a particle if hit
  51.                 Quaternion q = new Quaternion();
  52.                 q.SetLookRotation(hit.normal, Vector3.forward);
  53.                 particleClone = Instantiate(particle, hit.point, q);
  54.      
  55.                 //$$ agent.destination = hit.point;
  56.                 seeker.StartPath (transform.position, hit.point, OnPathComplete);
  57.             }
  58.         }
  59.      
  60.         public void OnPathComplete (Path p)
  61.         {
  62.             Debug.Log ("Yey, we got a path back (error = " + p.error + ")\n");
  63.             if (!p.error)
  64.             {
  65.                 path = p;
  66.      
  67.                 // Reset the waypoint counter
  68.                 currentWaypoint = 0;
  69.             }
  70.         }
  71.      
  72.         protected void SetupAgentLocomotion()
  73.         {
  74.             if (path == null)
  75.             {
  76.                 locomotion.Do(0, 0);
  77.                 if (particleClone != null)
  78.                 {
  79.                     GameObject.Destroy(particleClone);
  80.                     particleClone = null;
  81.                 }
  82.             }
  83.             else
  84.             {
  85.                 // Direction to the next waypoint
  86.                 Vector3 desiredVelocity = (path.vectorPath[currentWaypoint] - transform.position).normalized;
  87.      
  88.                 // Check if we are close enough to the next waypoint
  89.                 // If we are, proceed to follow the next waypoint
  90.                 if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance)
  91.                 {
  92.                     currentWaypoint++;
  93.                     if (currentWaypoint < path.vectorPath.Count)
  94.                     {
  95.                         Debug.Log ("Move to next waypoint : " + currentWaypoint + "\n");
  96.                     }
  97.                     else
  98.                     {
  99.                         Debug.Log ("End Of Path Reached\n");
  100.                         path = null;
  101.                     }
  102.      
  103.                 }
  104.      
  105.                 desiredVelocity *= speed ;
  106.      
  107.                 Vector3 velocity = Quaternion.Inverse(transform.rotation) * desiredVelocity; // desiredVelocity
  108.      
  109.                 float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
  110.      
  111.                 locomotion.Do(speed, angle);
  112.             }
  113.         }
  114.      
  115.         void OnAnimatorMove()
  116.         {
  117.             transform.Translate (animator.deltaPosition, Space.World);
  118.             transform.rotation = animator.rootRotation;
  119.         }
  120.      
  121.         // Update is called once per frame
  122.         void Update ()
  123.         {
  124.             if (Input.GetButtonDown ("Fire1"))
  125.             {
  126.                 if (path != null)
  127.                 {
  128.                     Debug.Log ("Path canceled (currentWaypoint = " + currentWaypoint + "/" + path.vectorPath.Count + ")\n");
  129.                     path = null;
  130.                 }
  131.                 SetDestination();
  132.             }
  133.      
  134.             SetupAgentLocomotion();
  135.         }
  136.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement