Astar movement implementation not working properly
I'm working on a RTS Project and I'd like to use the A* Project by arongranberg to navigate aroud abstacles, etc.
The problem is, that the correct path is displayed in the scene, but the unit just jumps to 0-0-0. The image displays the correct path, the unit is still walking forwards, as is used this as a workaround to have some progress on my build script.
Scene view
As shown in the picture, the path is shown correctly, the unit moves straigt through the object.
So, I don't know where I made the mistake that leads to this behaviour. I'll post the script responsible for the movement.
using UnityEngine;
using System.Collections;
using Pathfinding;
using RTS;
public class Unit : WorldObject
{
public float moveSpeed, rotateSpeed;
protected bool moving, rotating;
public Vector3 destination;
private Quaternion targetRotation;
private object destinationTarget;
private Seeker seeker;
private CharacterController controller;
public Path path;
public float nextWaypointDistance = 3;
private int currentWaypoint = 0;
protected override void Awake()
{
base.Awake();
}
protected override void Start()
{
base.Start();
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
}
protected override void Update()
{
base.Update();
if (path == null)
{
Debug.Log("Kein Pfad");
return;
}
if (currentWaypoint >= path.vectorPath.Count)
{
Debug.Log("Ende des Pfades erreicht: "+ path);
return;
}
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
// dir *= moveSpeed * Time.deltaTime;
if (rotating) TurnToTarget();
else if (moving) MakeMove(dir);
if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)
{
currentWaypoint++;
return;
}
}
protected override void OnGUI()
{
base.OnGUI();
}
public void newPath()
{
seeker.StartPath(transform.position, this.destination, OnPathComplete);
}
public void OnPathComplete(Path p)
{
Debug.Log("Pfad fertig. Fehler?: " + p.error);
if (!p.error)
{
path = p;
currentWaypoint = 0;
}
}
public override void MouseClick(GameObject hitObject, Vector3 hitPoint, Player controller)
{
base.MouseClick(hitObject, hitPoint, controller);
if (hasAuthority && player && player.human && currentlySelected)
{
if (hitObject.name == "Ground" && hitPoint != ResourceManager.InvalidPosition)
{
float x = hitPoint.x;
float y = hitPoint.y + player.SelectedObject.transform.position.y;
float z = hitPoint.z;
this.destination = new Vector3(x, y, z);
Debug.Log("Start_Move");
StartMove(destination);
}
}
}
public virtual void StartMove(Vector3 _destination)
{
Debug.Log(_destination+"----------------");
seeker.StartPath(transform.position, _destination, OnPathComplete);
destinationTarget = null;
targetRotation = Quaternion.LookRotation(destination - transform.position);
rotating = true;
moving = false;
}
private void TurnToTarget()
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotateSpeed);
Quaternion inverseTargetRotation = new Quaternion(-targetRotation.x, -targetRotation.y, -targetRotation.z, -targetRotation.w);
if (transform.rotation == targetRotation || transform.rotation == inverseTargetRotation)
{
rotating = false;
moving = true;
}
CalculateBounds();
}
private void MakeMove(Vector3 dir)
{
transform.position = Vector3.MoveTowards(transform.position, dir, Time.deltaTime * moveSpeed);
if (transform.position == destination) moving = false;
CalculateBounds();
}
}
I hope, you can find the problem here. If there is any information needed, please ask for it.
Ich würde mal raus loggen wenn nextWaypoint erhöht wird und wenn nextWaypointDistance tatsächlich 3 und der cube der zu sehen ist ein unit cube ist, dann würde ich sagen skipped er zu viele Wegpunkte.
P.S.: hab die deutschen Logs gesehen und musste einfach auf Deutsch antworten :)
Hm, das logging sieht i.O aus. Hab auch schon einge viele Stunden nach dem Fehler gesucht, auch schon via peer review (3h via skype) Edit: es wird ja jedes update eine direction übergeben, die auch empfangen wird, eigentlich sollte der weg sich jedes mal ändern. ich hatte vergessen, im code wieder das dir zu nehmen, da die geradeaus bewegeung nurd drin ist, damit ich an einem anderen skript weiterarbeiten konnte. Wenn ich den übergeben Vect3 dir nehme ($$anonymous$$ake$$anonymous$$ove methode), springt die unit einfach zu 0-0-0