Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class PathActor : MonoBehaviour {
- public Transform startt;
- public Transform destt;
- private PathManager pathManager;
- private List<Vector3> pathList = new List<Vector3>();
- public float nodeRadius = 1.5F;
- public float turnSpeed = 5;
- public float moveSpeed = 5;
- void Start () {
- pathManager = GetComponent<PathManager>();
- }
- void Update () {
- if (Input.GetMouseButtonUp(1)) {
- MouseTwoUp(Input.mousePosition);
- }
- if (pathList.Count != 0) {
- MoveToDest();
- }
- }
- void MouseTwoUp (Vector2 cPoint) {
- Ray ray = Camera.main.ScreenPointToRay(cPoint);
- RaycastHit hit;
- if (Physics.Raycast(ray,out hit)) {
- Vector3 dest = hit.point;
- dest = dest + hit.normal;
- GetPath(startt.position, dest);
- }
- }
- void GetPath (Vector3 start, Vector3 dest) {
- pathList.Clear();
- pathList = pathManager.FindPath(start, dest);
- Debug.Log("Calling from Actor returns : " + pathList.Count + " nodes in the path");
- }
- void MoveToDest () {
- if (pathList[0] == startt.position) pathList.RemoveAt(0);
- startt.rotation = Quaternion.Lerp (startt.rotation,
- Quaternion.LookRotation(pathList[0] - startt.position),
- Time.deltaTime * turnSpeed);
- startt.position += startt.forward * moveSpeed * Time.deltaTime;
- if(Vector3.Distance(startt.position, pathList[0]) < nodeRadius) {
- pathList.RemoveAt(0);
- Debug.Log(pathList.Count + " nodes left in pathList");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement