Units take the easy way out

Greetings,

I’ve been battling with the computer to make it bend to my will, but alas… So I’ve come before you to ask for your kindness, support and maybe some help.

Currently, that (^) is my start-up scene. But the moment I try to move the four buddies (above the white cube). They quit after moving a whole centimeter.

Their destination stands, but when I ask it to return me a log when it’s finished They’ll tell me they’re done after moving not much more than an inch (Shown at #2 on the screenshot.).

The way I make my units move is by right clicking on the terrain and letting the Seeker do it’s work. Worth noting that the units start at the same time - as I select them all and then give them a move order. This is the file attached to the four buddies:

using UnityEngine;
using System.Collections;
using Pathfinding;

public class UnitPath : MonoBehaviour
{
Seeker seeker;
CharacterController charControl;
Path path;
Unit unit;

[SerializeField]
float speed;
float nextWaypointDistance = 0.1f;

int currWaypoint = 0;

void Start()
{
	seeker = GetComponent<Seeker>();
	charControl = GetComponent<CharacterController>();
	unit = GetComponent<Unit>();
}

void FixedUpdate()
{
	if (!unit.GetWalkable)
		return;

	if (path == null)
		return;

	if (currWaypoint >= path.vectorPath.Count)
		return;

	Vector3 dir = (path.vectorPath[currWaypoint] - transform.position).normalized;
	dir *= speed * Time.deltaTime;
	charControl.Move(dir);

	if (Vector3.Distance(transform.position, path.vectorPath[currWaypoint]) < nextWaypointDistance)
		currWaypoint++;
}

void LateUpdate()
{
	if (unit.GetSelected && unit.GetWalkable)
	{
		if (Input.GetMouseButtonUp(1))
			seeker.StartPath(transform.position, Mouse.rightClickDown, OnPathComplete);
	}
}

void OnPathComplete(Path p)
{
	if (!p.error)
	{
		path = p;

		currWaypoint = 0;
		print("Cock");
	}
}
}

The settings for the seeker are found at #3 on the main screenshot.

The units, every move order they receive, move (even individually and not selected with other units) one node and then return they’ve completed. As an perceiving party, that is of course weird and illogical.

I’m at a loss and have pretty much searched the whole of google for other units taking the easy way out. Here’s hoping someone has an answer for me.

Thank you for your time,

  • Farcrada

EDIT: Solution has been found and applied.

For everyone that has taken time out of their day to replicate/look at/fix my problem, I found it (too).

The variable:

float nextWaypointDistance = 0.1f;

Is too small of a factor to connect/go to. I fixed this by setting this to:

float nextWaypointDistance = 5f;

And putting:

float nextWaypointDistance = defaultNextWaypointDistance;
if (currWaypoint == path.vectorPath.Count - 1)
    nextWaypointDistance = 0f;

right after:

charControl.Move(dir);




I also found out that:

void OnPathComplete(Path p)

Is called immediately after the calculations are completed, not the actual walking part. Something I hadn’t considered and overlooked.
Hence I thought that it had finished walking when in reality this was anything but the truth.

Thanks again,

  • Farcrada
1 Like

Great that you got it working!