A* Pathfinding Project

Accessing the next waypoint on a path (2D)


#1

I am using a custom AI in 2D. how would I find the point in which the unit is traveling? thanks.

here’s my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
using System;

public class EnemyAI : MonoBehaviour
{

public GameObject target;

public float speed = 200f;
public float nextWaypointDistance = 3f;

public Transform enemyGFX;

Path path;
int currentWayPoint = 0;
bool reachedEnd = false;

Seeker seeker;
Rigidbody2D rb;

public float dist;
public float seekDis;
// Start is called before the first frame update
void Start()
{
    seeker = GetComponent<Seeker>();
    rb = GetComponent<Rigidbody2D>();

    InvokeRepeating("UpdatePath", 0f, .5f);
}

private void Update()
{
    dist = Vector2.Distance(transform.position, target.transform.position);
}

void UpdatePath()
{
    if(seeker.IsDone())
    {
        seeker.StartPath(rb.position, target.transform.position, OnPathComplete);
    }
}

private void OnPathComplete( Path p) 
{
    if(!p.error)
    {
        path = p;
        currentWayPoint = 0;
    }
}

// Update is called once per frame
void FixedUpdate()
{
    if(path == null)
    {
        return;
    }
    if(currentWayPoint >= path.vectorPath.Count)
    {
        reachedEnd = true;
        return;
    }else
    {
        reachedEnd = false;
    }

    Vector2 direction = ((Vector2)path.vectorPath[currentWayPoint] - rb.position).normalized;
    Vector2 force = direction * speed * Time.deltaTime;

    if(dist <= seekDis)
    {
        rb.AddForce(force);
    }

    float distance = Vector2.Distance(rb.position, path.vectorPath[currentWayPoint]);

    if(distance < nextWaypointDistance)
    {
        currentWayPoint++;
    }
    Vector2 rotation = new Vector2(path.vectorPath[currentWayPoint].x, path.vectorPath[currentWayPoint].y);
    Debug.Log(rotation);
    transform.eulerAngles = new Vector3 (0,0, Vector2.Angle(rb.position, ));
    
    
}

}


#2

The point the agent is moving towards is just

path.vectorPath[currentWayPoint]

#3

thanks. I suspected it but wasn’t sure