i’m trying to get an enemy ai to go from point to point in a way point system.
i’ve tried two different way’s and neither of them have really worked out well enough for me so far. i’m trying to push the enemy around as well via force and rigid body physics.
can anyone help with the code to get it to work properly.
Hi, this is probably a very simplistic approach but worked fine for me. Would be great if someone with better programming skills helps to refine this…
Hope it helps
Juan
private var hasReachedLastWaypoint : boolean = false;
var minimunDistance = 7;
//just some game objects - you need only the transform position information
var waypointPath : Transform[];
while(true)
{
if(someCondition)//this is necesary only if you want some condition to trigger the following path behavior
{
FollowWaypointPath(waypointPath);
}
yield;
}
}
function FollowWaypointPath(waypointArray : Transform[])
{
var curWaypoint : int = 0;
StartCoroutine("Approach",waypointArray[curWaypoint]);
while(true)
{
if(Vector3.Distance(transform.position,waypointArray[curWaypoint].position) < 0.5)
{
StopCoroutine("Approach");
if(curWaypoint < waypointArray.length -1)
{
curWaypoint++;
hasReachedLastWaypoint = false;
StartCoroutine("Approach",waypointArray[curWaypoint]);
} else {
StopCoroutine("Approach");
hasReachedLastWaypoint = true;
}
}
yield;
}
}
function Approach(theTarget : Transform)
{
while(true)
{
if(Vector3.Distance(transform.position,theTarget.position) < minimunDistance)
{
var direction = theTarget.position - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0,transform.eulerAngles.y,0);
var forward = transform.TransformDirection(Vector3.forward);
direction = forward * walkingSpeed;
GetComponent(CharacterController).SimpleMove(direction);
animation.Play("walk");
}
yield;
}
}
Glad to help
I am not very keen on moving physic based agents, but at least theoretically it should work using forces instead of transform based movement…
just add all your waypoint (gameObjects) into the waypoint slot. The thing this script is attached to well fallow the waypoints. You can mess with some var for lurp, rotation and dam. Make the transition from way point to way point more easy.