- Home /
How to make enemy attack multiple unit rather then single?(updated)
Hi guys, the question is how to make the enemy continue search its target after its initial target has been killed? (UPDATED: i have successfully make the AI able to kill its target and go back to its waypoint but now the problem is it doesn't bother about the other target which carry the same tag, how do i make it hit other target as well?)
var targetObject : GameObject;
function DetectEnemy()
{
if (targetObject == null && GameObject.FindWithTag("Player1"))
targetObject = GameObject.FindWithTag("Player1");
}
function Start () {
// Auto setup player as target through tags
Patrol();
}
function Patrol () {
var curWayPoint = AutoWayPoint1.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 {
DetectEnemy();
if (Vector3.Distance(transform.position, targetObject.transform.position) > attackRange)
return false;
var hit : RaycastHit;
if (Physics.Linecast (transform.position, targetObject.transform.position, hit))
return hit.transform == targetObject.transform;
return false;
}
function AttackPlayer () {
var lastVisiblePlayerPosition = targetObject.transform.position;
while (true) {
if (CanSeeTarget ()) {
// Target is dead - stop hunting
if (targetObject.transform == null)
return;
// Target is too far away - give up
var distance = Vector3.Distance(transform.position, targetObject.transform.position);
if (distance > shootRange * 3)
return;
lastVisiblePlayerPosition = targetObject.transform.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);
Debug.Log("Test inside Attack 1");
// Player not visible anymore - stop attacking
if (!CanSeeTarget ())
return;
}
yield;
}
}
function SearchPlayer (position : Vector3) {
// Run towards the player but after 3 seconds timeout and go back to Patroling
var timeout = 3.0;
while (timeout > 0.0) {
MoveTowards(position);
// We found the player
if (CanSeeTarget ())
return;
timeout -= Time.deltaTime;
yield;
}
}
Don´t expect precise answers when you post all your code like that, I didn´t even read it but I would be surprised that your problem was a little more than just having a reference to the enemies that you want to attack. Personally when I make the "enemy" class I create a static list that has a reference to all of them and then check which are in range when I attack, however that may be too expensive depending on the game your are developing.
yea i know this is a long code i have shortened it because i afraid i might miss few important code, actually tats is my problem i don't know how to implement a static or array list to my enemy can you help me please?
guys i have updated the code, but still no luck has been found any1 know the solution?
Answer by Vandarthul · Dec 10, 2013 at 04:42 PM
Try creating a gameobject array that holds every enemy in range. And check that array after you killed an enemy, if it's not 0 continue to attack, if it's 0 turn to waypoint.
I'm sorry I don't have time to go through your code and give the exact answer but I can give you a place to start.
Hi sir do you know how i can invoke or call another script from this script?
Hi guys i have add the array list to my AI but right now the AI just stop at there. Anyone know why? Below is the updated detectenemy for the above code
public var targetObject : GameObject[];
function DetectEnemy()
{
if (targetObject == null && GameObject.FindGameObjectsWithTag("Player1"))
targetObject = GameObject.FindGameObjectsWithTag("Player1");
for(var i : int = 0; i < targetObject.Length; i++)
{
Patrol();
}
}
Your answer
Follow this Question
Related Questions
Waypoint System help 1 Answer
How to make the enemy move back to its waypoint after it's target killed? 2 Answers
AI Waypoint help or suggestions!? 2 Answers
How do I make an enemy lead his shots? 2 Answers
How do I create a simply disable script for enemy AI once it completes its waypoints? 2 Answers