I am working on a game with spherical worlds. I am using the following script to chase down enemies. I have no idea how to implement flee path along with AIPath, so that AI can resist certain class of character.
public class AI_Controller : AIPath
{
Vector3 interpolatedUp = Vector3.up;
Transform SetTarget(string tagName)
{
GameObject closestObj = null;
float closestDist = -1;
List<GameObject> objs = new List<GameObject>(GameObject.FindGameObjectsWithTag(tagName));
foreach (GameObject go in objs)
{
if (go.GetComponent<BoltEntity>().GetState<IPlayerState>().isDead)
continue;
float dist = Vector3.Distance(transform.position, go.transform.position);
if (dist < closestDist || closestDist < 0)
{
closestDist = dist;
closestObj = go;
}
}
if (closestObj != null)
return closestObj.transform;
return null;
}
protected override void Update()
{
base.Update();
switch (tag)
{
case "Paper":
target = SetTarget("Rock");
break;
case "Rock":
target = SetTarget("Scissor");
break;
case "Scissor":
target = SetTarget("Paper");
break;
}
tr.position = RaycastPosition(tr.position);
}
protected override IMovementPlane MovementPlaneFromNode(GraphNode node)
{
var forward = Vector3.Cross(Vector3.right, interpolatedUp);
return new GraphTransform(Matrix4x4.TRS(Vector3.zero, Quaternion.LookRotation(forward, interpolatedUp), Vector3.one));
}
/** Find the world position of the ground below the character */
Vector3 RaycastPosition(Vector3 position)
{
RaycastHit hit;
var normal = interpolatedUp;
if (Physics.Raycast(position + tr.up * 0.5f, -tr.up, out hit, 2f, groundMask))
{
normal = hit.normal;
position = hit.point;
}
// Use the node surface as the movement plane
interpolatedUp = Vector3.Slerp(interpolatedUp, normal, 4 * Time.deltaTime);
return position;
}