Hello! I am making a basic RTS engine using the A* Pathfinding project and I have ran into a bit of a dead end. The unit goes where I tell it to only when selected but the problem is that it stops moving if I deselect it. In other words for the unit to move the player needs to keep it selected.
The following is my Selectable.cs
using UnityEngine;
using System.Collections;
public class Selectable : MonoBehaviour {
//Selected
public bool selected = false;
//Team and Alliance
public int team= 0; //Team is the player who the unit is loyal to
public int alliance= 0;//Alliance is the group of players it is loyal to
//Set the color of the unit to the team color
void Start (){
}
//If a unit is selected then enable its Projector to show it
void Update (){
if(selected){
if(!transform.Find("Plane").GetComponent<MeshRenderer>().enabled)
transform.Find("Plane").GetComponent<MeshRenderer>().enabled = true;
}
else{
if(transform.Find("Plane").GetComponent<MeshRenderer>().enabled)
transform.Find("Plane").GetComponent<MeshRenderer>().enabled = false;
}
}
}
and my actual AstarAI.cs
using System.Collections;
//Note this line, if it is left out, the script won't know that the class 'Path' exists and it will throw compiler errors
//This line should always be present at the top of scripts which use pathfinding
using Pathfinding;
public class AstarAI : MonoBehaviour {
//The point to move to
public Vector3 targetPosition;
private Seeker seeker;
private CharacterController controller;
public Selectable selectable;
//The calculated path
public Path path;
//The AI's speed per second
public float speed = 100;
RaycastHit hit;
Ray ray;
//The max distance from the AI to a waypoint for it to continue to the next waypoint
public float nextWaypointDistance = 3;
//The waypoint we are currently moving towards
private int currentWaypoint = 0;
float raycastlength = 1000;
public void Start () {
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
selectable = GetComponent<Selectable>();
//Start a new path to the targetPosition, return the result to the MyCompleteFunction
seeker.StartPath (transform.position, targetPosition, MyCompleteFunction);
}
public void MyCompleteFunction (Path p) {
Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
if (!p.error) {
path = p;
//Reset the waypoint counter
currentWaypoint = 0;
}
}
public void Update () {
if (path == null) {
//We have no path to move after yet
return;
}
if (selectable.selected == true)
{
if (Input.GetButtonDown("Fire1"))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit, raycastlength))
{
targetPosition = hit.point;
seeker.StartPath (transform.position, targetPosition, MyCompleteFunction);
}
}
if (currentWaypoint >= path.vectorPath.Length) {
Debug.Log ("End Of Path Reached");
return;
}
//Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
dir *= speed * Time.deltaTime;
controller.SimpleMove (dir);
if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
currentWaypoint++;
return;
}
}
}
}
Any help in how to fix this would mean alot to me! Thank you ![]()