I have been trying to get a simple AI working with Angry Ant's path tool, I've followed the tutorial so I've got a path to follow but I cannot work out how to make the object follow the it. Is there a simple example project available so I can see it in action or could someone please tell me how access the path data?
I finally got the answer to my own question. For those who wish to know, the two lines I was missing are as follows;
targWayFrom = m_CurrentPath.Segments[WayCnt].From as Waypoint;
targWayTo = m_CurrentPath.Segments[WayCnt].To as Waypoint;
They work from within the DemoSeeker script. WayCnt is the current connection you are accessing, the first position the object moves to is Segments[0].From
This was the script I got working. It's designed to use the Mixamo zombie character. I had to edit the zombie controller to accept script driven stimulus instead of a keystroke. I'll be doing a tutorial on how to make path and behave work together and I'll post a link here if I can. I don't do a lot of coding in summer so it will have to wait a bit.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Navigator))]
public class DemoSeeker : MonoBehaviour
{
public Transform m_Target;
public Path m_CurrentPath;
public float speed = 3.0F;
public float rotateSpeed = 3.0F;
public float ZombieRotate = 0.0F;
public float targetDir=0.0f;
public Vector3 targVect=Vector3.zero;
public int PathCnt;
public int WayCnt;
public int NodeCnt;
public Connection[] WaypointObj;
public int WaypointCnt;
public int WayPathCnt;
public Connection targObj;
public Waypoint targWayFrom;
public Waypoint targWayTo;
public float WayDist;
public bool ZombieMove=false;
public static bool ZomFwd=false;
public static bool ZomLft=false;
public static bool ZomRgt=false;
public static Vector3 RelativePos;
public int TurnCnt=0;
void Start ()
{
m_Target=GameObject.FindWithTag("Player").transform;
GetComponent<Navigator> ().RegisterWeightHandler ("Water", OnHandleWaterWeight);
GetComponent<Navigator>().targetPosition=m_Target.position;
GetComponent<Navigator>().ReSeek();
PathCnt=0;
WayCnt=0;
NodeCnt=0;
TurnCnt=0;
}
// Update is called once per frame
void Update ()
{
PathCnt++;
if(PathCnt==5)
{
targWayFrom = m_CurrentPath.Segments[WayCnt].From as Waypoint;
targWayTo = m_CurrentPath.Segments[WayCnt].To as Waypoint;
ZombieMove=true;
PathCnt=0;
if ((transform.position.x<targWayFrom.Position.x+2)&&(transform.position.x>targWayFrom.Position.x-2)&&(transform.position.z<targWayFrom.Position.z+2)&&(transform.position.z>targWayFrom.Position.z-2))
{
WayCnt++;
}
}
if(ZombieMove==true)
{
ZomRgt=false;
ZomLft=false;
TurnCnt++;
ZomFwd=true;
if(TurnCnt==5)
{
TurnCnt=0;
RelativePos=transform.InverseTransformPoint(targWayFrom.Position);
if (RelativePos.x > 0)
{
ZomRgt=true;
ZomLft=false;
}
if (RelativePos.x < 0)
{
ZomRgt=false;
ZomLft=true;
}
}
}
}
void OnNewPath (Path path)
// When pathfinding via Navigator.targetPosition
{
Debug.Log ("Received new Path from " + path.StartNode + " to " + path.EndNode + ". Took " + path.SeekTime + " seconds.");
m_CurrentPath = path;
}
void OnTargetUnreachable ()
// When pathfinding via Navigator.targetPosition
{
Debug.Log ("Could not pathfind to target position");
m_CurrentPath = null;
}
void OnPathAvailable (Path path)
// When pathfinding via Navigator.RequestPath (startPositio, endPosition)
{
Debug.Log ("Requested Path from " + path.StartNode + " to " + path.EndNode + " is now available. Took " + path.SeekTime + " seconds.");
}
void OnPathUnavailable ()
// When pathfinding via Navigator.RequestPath (startPositio, endPosition)
{
Debug.Log ("The requested path could not be established.");
}
void OnPathInvalidated (Path path)
// When a path requested by a Navigator on this GameObject is no longer valid - due to a connection or node disabling or removal
{
Debug.Log ("The path from " + path.StartNode + " to " + path.EndNode + " is no longer valid.");
}
void OnGUI ()
{
if (GUILayout.Button ("Pathfind"))
{
GetComponent<Navigator> ().targetPosition = m_Target.position;
}
if (GUILayout.Button ("ReSeek"))
{
GetComponent<Navigator> ().ReSeek ();
}
}
void OnDrawGizmos ()
{
if (m_CurrentPath == null)
{
return;
}
m_CurrentPath.OnDrawGizmos ();
}
float OnHandleWaterWeight (object obj)
{
return 3.0f;
}
}
P.s. Mr Ant, if you happen to read this, your path system is smarter than my dog. She managed to get herself stuck in a tennis court with an open gate this morning :-)