- Home /
Waypoint movement A*
Hi, I've struggled with this for ages but I haven't ever given enough details, so I hope this is a clearer question. The waypoint A* works perfectly fine, up until you click elsewhere and the player travels to this new point but sometimes misses out the nearest node for some reason. The A* logic is in another file but is working fine i believe. It's the movement code that is the issue. (currentPath is the stack of nodes for the player to traverse and PathManager is the name of the script with the A* logic)
Movement code:
*public class PlayerController : MonoBehaviour {
Vector3 targetPosition;
public Nodes finalPos;
public Nodes currentPos;
public float speed = 5;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
SetTargetPosition();
PathManager.NavigateTo(finalPos.transform.position);
}
if (PathManager.currentPath != null && PathManager.currentPath.Count > 0)
{
if (PathManager.currentPath.Count == 0)
{
Stop();
}
if (transform.position == PathManager.currentPath.Peek())
{
PathManager.currentPath.Pop();
}
if (transform.position != PathManager.currentPath.Peek())
{
transform.position = Vector3.MoveTowards(transform.position, PathManager.currentPath.Peek(), speed * Time.deltaTime);
}
}
}
public void Stop()
{
PathManager.currentPath = null;
}
void SetTargetPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
targetPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
finalPos = PathManager.FindClosestNode(targetPosition);
}
}
}*
NavigateTo is a PathManager program that calculates the stack of nodes that form the quickest route. Any help is appreciated.
It misses out the nearest node when you click to go to another node (hence a new path midway). Almost like it's calculated that the hypothenuse is quicker, (it happens only on corners)
Your answer
Follow this Question
Related Questions
Enemy Follow Player and a path with IEnumerator 0 Answers
Third Person Script - moving+rotating without WASD 0 Answers
Problems with diagonal movement. 1 Answer
Hello Im making a Game for mobile and want two floating joysticks one on either side of the screen. 0 Answers
Help My movement is broken!! 2 Answers