I have bin trying to make a node walkable on runtime.
I set it to walkable and the node dissapears, but when i set a destination and activate it to move i am getting an error.
“No open points, the start node didn’t open any nodes”
i checked the code and is working when they are not on the “changed” blocks.
AI code.
`
using UnityEngine;
using System.Collections;
//Note this line, if it is left out, the script won’t know that the class ‘Path’ exists and it will throw compiler errors
//This line should always be present at the top of scripts which use %Pathfindingusing
using Pathfinding;
public class AstarAI : MonoBehaviour
{
//The point to move to
public Vector3 targetPosition;
private Seeker seeker;
private CharacterController controller;
//The calculated path
public Path path;
//The AI’s speed per second
public int speed = 0;
//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 void Start ()
{
speed = 10;
seeker = GetComponent();
controller = GetComponent();
}
public void SetWalk(Vector3 pos, Bounds bou)
{
//NNInfo node = AstarPath.active.GetNearest(pos); //node.node.Walkable = true;
AstarPath.RegisterSafeUpdate (delegate () {
NNInfo node = AstarPath.active.GetNearest(pos);
node.node.walkable = true;
},true);
//GraphUpdateObject ob = new GraphUpdateObject(tra.collider.bounds);
//AstarPath.active.UpdateGraphs(ob);
//AstarPath.active.UpdateGraphs (bou);
}
public void StartSeeking()
{
//Start a new path to the targetPosition, return the result to the OnPathComplete function
seeker.StartPath (transform.position,targetPosition, OnPathComplete);
}
public void OnDisable ()
{
seeker.pathCallback -= OnPathComplete;
}
public void OnPathComplete (Path p)
{
Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
if (!p.error)
{
path = p;
//Reset the waypoint counter
currentWaypoint = 0;
}
}
public void FixedUpdate ()
{
if (path == null)
{
//We have no path to move after yet
return;
}
if (currentWaypoint >= path.vectorPath.Count)
{
GetComponent<DemoWander>().PathMoving = false;
GetComponent<DemoWander>().PickNewAction();
Debug.Log ("End Of Path Reached");
return;
}
//Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
controller.SimpleMove (dir);
//Check if we are close enough to the next waypoint
//If we are, proceed to follow the next waypoint
if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance)
{
currentWaypoint++;
return;
}
}
}
`
here are the lines from where i call a function to make them walkable.
1 for the tile he is standing on and 1 for the one in front of him.
Change: seeker.StartPath (transform.position,targetPosition, OnPathComplete); to seeker.StartPath (transform.position,targetPosition);
Second I don’t know the official way to make a node walkable again, but for the time being I am just doing the following go.layer = 0; AstarPath.active.UpdateGraphs(go.collider.bounds); Destroy(go)
That seems to be the most effective solution I have found so far. It’s simple and doesn’t seem to be much of a performance hit.
If this doesn’t answer your question please elaborate more on your code and desired effect.
I have these blocks as seen in the screenshot, they are a mesh together defided in chunks.
once a block i changed by the AI it turns flat, and at that point the i do not want to rescan but rather change that single block to walkable.
the blocks are not gameobjects and i cannot acces them individually.
There is an array though where i can change them separately but that is through their own class so i cannot acces the transform etc.
in the screenshot above i did not use “UpdateGraphs” and by not using that the nodes dissapear as they should.
However when i do use it this is what happens:
Note: the AI moves blocks by block, it flattens a block then moves to the next etc.
I see, yes that is much clearer, using walkable is correct but there is much more to it than that. The unwalkable nodes I assume are unwalkable set by scanning? If that is the case then even making a node walkable isn’t enough. Because yes it will be walkable but it will have 0 connections. so you would have to add a connection
node.AddConnection(otherNode, cost)
hmm from what I can find that should do the trick.
If not, then I am sure Aron would be able to give you a much better (and working) answer.
I’m still fairly new to this myself, But figure I’ll attempt to try when I can.
I am trying to get it working, but it throws me an error.
GridNodes do not have support for adding manual connections
public override void AddConnection (GraphNode node, uint cost) { throw new System.NotImplementedException("GridNodes do not have support for adding manual connections"); }
Thanks again for your time.
I will be trying more aswell and if i fix it i will post it here.