- Home /
How to make the enemy move back to its waypoint after it's target killed?
Hi guys i have implemented the fps_tutorial AI script and its working for 2 enemy fighting each other. The problem is once the target is dead, the enemy will not go to its waypoint and just stuck there anyone have solution for it? I think is something to do with the array list but due to my limited knowledge i don't really know how to make one, can somebody help me please?
function Start () {
// Auto setup player as target through tags
if (target == null && GameObject.FindWithTag("Player1"))
target = GameObject.FindWithTag("Player1").transform;
Patrol();
}
function Patrol () {
var curWayPoint = AutoWayPoint.FindClosest(transform.position);
while (true) {
var waypointPosition = curWayPoint.transform.position;
// Are we close to a waypoint? -> pick the next one!
if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance)
curWayPoint = PickNextWaypoint (curWayPoint);
// Attack the player and wait until
// - player is killed
// - player is out of sight
if (CanSeeTarget ())
yield StartCoroutine("AttackPlayer");
// Move towards our target
MoveTowards(waypointPosition);
yield;
}
}
function CanSeeTarget () : boolean {
if (Vector3.Distance(transform.position, target.position) > attackRange)
return false;
var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;
return false;
}
function AttackPlayer () {
var lastVisiblePlayerPosition = target.position;
while (true) {
if (CanSeeTarget ()) {
// Target is dead - stop hunting
if (target == null)
Patrol();
// Target is too far away - give up
var distance = Vector3.Distance(transform.position, target.position);
if (distance > shootRange * 3)
return;
lastVisiblePlayerPosition = target.position;
if (distance > dontComeCloserRange)
MoveTowards (lastVisiblePlayerPosition);
else
RotateTowards(lastVisiblePlayerPosition);
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = lastVisiblePlayerPosition - transform.position;
targetDirection.y = 0;
var angle = Vector3.Angle(targetDirection, forward);
// Start shooting if close and play is in sight
if (distance < shootRange && angle < shootAngle)
yield StartCoroutine("Shoot");
} else {
yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
// Player not visible anymore - stop attacking
if (!CanSeeTarget ())
return;
}
yield;
}
}
Answer by aldonaletto · Dec 09, 2013 at 02:20 PM
I don't know whether this will solve your problem, but I would just return when the enemy is dead:
// Target is dead - stop hunting
if (target == null)
return; // just return to resume the Patrol coroutine
my earlier code was return but that doesnt work either, if im not mistaken the enemy should been an array list is just that i don't know how to code it. i found example but i don't know how to implement along with this script as there is some error on this example script.
function Start () {
var targets : Transform[] = new Transform[];
var selectedtarget : Transform;
selectedtarget = null;
var go : GameObject[] = new GameObject[];
go = GameObject.FindGameObjectsWithTag("Enemy");
var length = go.Length;
for (var i = 0; i < length; i++) {
if(go[i].tag=="Enemy")
targets.SetValue(go[i].transform);
}
var num = targets.Length;
var rnd : System.Random = new System.Random;
var ran = rnd.Next(0,num);
selectedtarget = targets[ran];
}
Do you want to pass to the next enemy or return to the last waypoint when the current enemy has been killed?
if i wan both? if is too complicated den just return to the last waypoint will do
Is any error being reported in the console? I would check the target existence before using CanSeeTarget in order to avoid errors:
...
while (true) {
// Stop hunting if target is dead:
if (target == null) return;
if (CanSeeTarget ()) {
// Target is too far away - give up
var distance = Vector3.Distance(transform.position, target.position);
if (distance > shootRange * 3)
return;
...
hi there there is no error but the enemy just stuck at there after the target has been destroyed ins$$anonymous$$d of going to its waypoint
Answer by Java666 · Dec 30, 2013 at 06:38 AM
i got my enemies going back to waypoints but i using navmesh to SetDestination( route[currentPoint].position); but for yours looks like you should just take patrol function out of start and set it to run in update is target is null
Your answer
Follow this Question
Related Questions
Waypoint System help 1 Answer
How to make enemy attack multiple unit rather then single?(updated) 1 Answer
Problem with the movement of AI 1 Answer
Enemy not rotating when inside range 1 Answer
AI Waypoint help or suggestions!? 2 Answers