Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class PathActor : MonoBehaviour {
  6.    
  7.     public Transform startt;
  8.     public Transform destt;
  9.  
  10.     private PathManager pathManager;
  11.     private List<Vector3> pathList = new List<Vector3>();
  12.    
  13.     public float nodeRadius = 1.5F;
  14.     public float turnSpeed = 5;
  15.     public float moveSpeed = 5;
  16.    
  17.     void Start () {
  18.         pathManager = GetComponent<PathManager>();
  19.     }
  20.    
  21.     void Update () {
  22.         if (Input.GetMouseButtonUp(1)) {
  23.             MouseTwoUp(Input.mousePosition);
  24.         }
  25.        
  26.         if (pathList.Count != 0) {
  27.                 MoveToDest();
  28.         }
  29.     }
  30.    
  31.     void MouseTwoUp (Vector2 cPoint) {
  32.         Ray ray = Camera.main.ScreenPointToRay(cPoint);
  33.         RaycastHit hit;
  34.        
  35.         if (Physics.Raycast(ray,out hit)) {
  36.             Vector3 dest = hit.point;
  37.             dest = dest + hit.normal;
  38.             GetPath(startt.position, dest);
  39.         }
  40.     }
  41.    
  42.     void GetPath (Vector3 start, Vector3 dest) {
  43.         pathList.Clear();
  44.         pathList = pathManager.FindPath(start, dest);
  45.         Debug.Log("Calling from Actor returns : " + pathList.Count + " nodes in the path");
  46.     }
  47.    
  48.     void MoveToDest () {
  49.         if (pathList[0] == startt.position) pathList.RemoveAt(0);
  50.            
  51.         startt.rotation = Quaternion.Lerp (startt.rotation,
  52.                                            Quaternion.LookRotation(pathList[0] - startt.position),
  53.                                            Time.deltaTime * turnSpeed);
  54.            
  55.         startt.position += startt.forward * moveSpeed * Time.deltaTime;
  56.            
  57.         if(Vector3.Distance(startt.position, pathList[0]) < nodeRadius) {
  58.             pathList.RemoveAt(0);
  59.             Debug.Log(pathList.Count + " nodes left in pathList");
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement