I’ve gotten the AStar pathfinding up to this point. My character will move to the location that I click, or more specifically the node. Now I need the last part of the puzzle to really get cranking on my game. I need to figure out how to make it so when I click an object (like a tree or level) the character will navigate to the nearest square to the object, but not try to go to the object itself. I’m not sure what the best methode to use is, in the past I would use a raycast and when I got close enough to the clicked object my character would stop moving, but I found this to be a very clunky system and only worked partially. Is there a good way to set it up in the code I have listed below to make a player move to the nearest node to the object? Any help at all would be great!
[CODE]
using Pathfinding;
public class AstarAI : MonoBehaviour
{
//The point to move to
public Vector3 targetPosition;
public Transform targetTransform;
private Seeker seeker;
private CharacterController controller;
//The calculated path
public Path path;
//The AI's speed per second
public float speed = 20;
//The max distance from the AI to a waypoint for it to continue to the next waypoint
public float nextWaypointDistance = 3;
//The waypoint we are currently moving towards
private int currentWaypoint = 0;
public float repathRate = 0.5f;
private float lastRepath = -9999;
//tag checks
public string tagCheckwalkable = "walkable";
public string tagCheckuseable = "useable";
public string tagCheckenemy = "enemy";
public void Start()
{
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
//Start a new path to the targetPosition, return the result to the OnPathComplete function
//seeker.StartPath (transform.position,targetPosition, OnPathComplete);
}
public void OnPathComplete(Path p)
{
p.Claim(this);
if (!p.error)
{
if (path != null) path.Release(this);
path = p;
//Reset the waypoint counter
currentWaypoint = 0;
}
else
{
p.Release(this);
Debug.Log("Oh noes, the target was not reachable: " + p.errorLog);
}
//seeker.StartPath (transform.position,targetPosition, OnPathComplete);
}
public void Update()
{
//if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
targetTransform = hit.transform;
if (hit.transform.tag == tagCheckwalkable)
{
targetPosition = hit.point;
}
if (hit.transform.tag == tagCheckuseable)
{
//GraphNode node = AstarPath.active.GetNearest(transform.position).node;
}
}
if (Time.time - lastRepath > repathRate && seeker.IsDone())
{
lastRepath = Time.time + Random.value * repathRate * 0.5f;
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
if (path == null)
{
//We have no path to move after yet
return;
}
if (currentWaypoint > path.vectorPath.Count) return;
if (currentWaypoint == path.vectorPath.Count)
{
Debug.Log("End Of Path Reached");
currentWaypoint++;
return;
}
//Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed;// * Time.deltaTime;
//transform.Translate (dir);
controller.SimpleMove(dir);
if ((transform.position - path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance * nextWaypointDistance)
{
currentWaypoint++;
return;
}
}
}
}[/CODE]