It’s a top-down RTS-style interface in a 2d environment. I’m trying to get it so that when the background (represented by a quad with a mesh collider) gets clicked on, the selected units move to the cursor location, while if a unit gets clicked on, the selected units move to the location of the clicked unit:
public void thingRightClicked (GameObject thingClicked) {
Vector3 destination;
switch (thingClicked.tag) {
case "unit":
destination = thingClicked.transform.position;
break;
case "ground":
destination = Input.mousePosition;
break;
default:
destination = new Vector3(0,0,0);
break;
}
foreach (GameObject unit in gameState.getActiveUnits()) {
unit.GetComponent<Unit>().move(destination);
Debug.Log("Called unit.move");
}
}
From the movement script in the unit game object.
void Start() {
seeker = GetComponent<Seeker>();
}
public void setDestination (Vector3 destination) {
seeker.StartPath(transform.position, destination, OnPathComplete);
Debug.Log("Destination set.");
}
void OnPathComplete (Path finishedPath) {
Debug.Log("Yay, we got a path back. Did it have an error? " + finishedPath.error);
path = (ABPath) finishedPath;
}
The program only malfunctions when the ground is clicked on: when the seeker.Startpath method gets called with the mouse location as a parameter, it does return a path, but the path has zero points in its vectorPath list. An ArgumentOutOfRangeException occurs soon thereafter.
Hey! That was it! I’m a bit of a novice, as you can probably tell. I am still curious about why it wasn’t working, though. Even though the coordinates that A* was receiving were very wrong, they were still usable X-Y coordinates, right? Why wasn’t the game object able to just head off towards wherever those coordinates were in world terms?
*edit: @Cannist Actually, now the navigation works the first time, but it throws the same error if I give the unit a second move command to another non-unit point. Interestingly, it will not throw this error if I give the new instruction before it arrives at the initial destination. Any ideas? Here’s a more complete version of my movement script; the error occurs on line 12:
void Update() {
if (path == null) {
return;
}
if (Vector2.Distance(transform.position, path.endPoint) < roundToArrived) {
path = null;
Debug.Log("Destination reached. Path null.");
return;
}
for (int i = 0; i < 1000; i++) {
//if you are within a specified range of the next waypoint
if (Vector2.Distance(transform.position, path.vectorPath[currentWaypoint]) < changePointThreshhold) {
//and if the number of the next waypoint would not exceeed the number of waypoints in the path
if (currentWaypoint + 1 < path.vectorPath.Count) {
//increment the currentWaypoint (I think there should be another break here, but it's not in the example)
currentWaypoint++;
}
else {
//end reached
path = null;
Debug.Log("Destination reached exactly. Path null.");
break;
}
}
else {
//no incrementing needed yet
break;
}
}
Vector3 dirNew = (path.vectorPath[currentWaypoint] - transform.position).normalized;
transform.position += dirNew * speed * Time.deltaTime;
}
Well, the input mouse position gives the x- and y- coordinates in screen space, which means that the y-coordinate is somewhere between 0 and 1080 on a 1920x1080 monitor. For your world space, the y-axis is up, meaning that the coordinates probably were way up in the sky, which probably wouldn’t be close enough to anything navigable to be of use.
You generally want to raycast against the ground with the mouse position to find where to move to.
For your updated question, “throws an error” isn’t really much to go about with help. It essentially boils down to “here’s a pile of code, there’s something wrong somewhere”. The type of error and line number would make it possible to help you.
@Baste Sorry if I wasn’t clear enough in my OP: I’m getting an ArgumentOutOfRangeException on line 12 of the above Update function. This is because it is trying to access entry 0 of a vectorPath list that is completely empty. The problem is somewhere in the path creation, which isn’t part of my code but has something to do with how I’m using the A* plugin.