- Home /
Hi i need help, how to make the player attack the next enemy with the same tag one by one until all is destroyed,How to destroy all the objects with the same tag one by one until no one is left
The problem with this script is after killing its first victim it does not search anymore another enemy with the same tag, i need help for my player to search and destroy the remaining enemies with the same tag killing one by one until no remaining enemy is left, thank you..
pragma strict
/ THIS IS THE MAIN SCRIPT TO CONTROL AI BEHAVIOR AND AUTO SHOOT ENEMY WHEN IN RANGE AND AUTO NAVIGATE AND FIND ENEMY WHEN OUT OF RANGE /
//var TargetToFollowPosition : Transform; var FollowSpeed : float ; private var mySwitch : boolean; private var shootSwitch : boolean; private var targetToFollow : Transform; private var numSwitch : float;
private var bulletShotSound : AudioSource;
var projectile : Rigidbody; var BulletSpeed : float; var BulletInterval :float; var Spawnpoint : Transform;
private var TimeContainer : float;
function Start () { bulletShotSound = gameObject.GetComponent.(); targetToFollow = GameObject.FindGameObjectWithTag("Enemy").transform; numSwitch = 0; //mySwitch = false; shootSwitch = false; //speed = 3; }
///// COLLISION DETECTOR WHEN ENEMY IS IN RANGE function OnTriggerStay(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
// mySwitch = true;
shootSwitch = true;
numSwitch = 1;
}
}
//////// DETECTION WHEN NOT IN RANGE function OnTriggerExit(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
// mySwitch = false;
shootSwitch = false;
numSwitch = 0;
}
}
function Update () { ////// UPDATE ////////////
TimeContainer += Time.deltaTime;
///////// ENABLE FIRING if(shootSwitch == true) {
if(TimeContainer >= BulletInterval) {
bulletShotSound.Play(); //var clone : Rigidbody; var clone = Instantiate(projectile, Spawnpoint.position, projectile.rotation); clone.velocity = Spawnpoint.TransformDirection (Vector3.forward*BulletSpeed); Destroy(clone,1.0f); TimeContainer = 0; }
}
////// FOLLOW ENEMY WHILE NOT IN RANGE if(numSwitch == 0) {
transform.LookAt(targetToFollow); transform.position += transform.forward FollowSpeed Time.deltaTime; }
} ////// UPDATE ///////////,The problem here is, the player will stop looking other enemy objects after it kills its first victim, i need a script that will enable the player to seek and destroy all the remaining enemies, thanks
pragma strict
/ THIS IS THE MAIN SCRIPT TO CONTROL AI BEHAVIOR AND AUTO SHOOT ENEMY WHEN IN RANGE AND AUTO NAVIGATE AND FIND ENEMY WHEN OUT OF RANGE /
//var TargetToFollowPosition : Transform; var FollowSpeed : float ; private var mySwitch : boolean; private var shootSwitch : boolean; private var targetToFollow : Transform; private var numSwitch : float;
private var bulletShotSound : AudioSource;
var projectile : Rigidbody; var BulletSpeed : float; var BulletInterval :float; var Spawnpoint : Transform;
private var TimeContainer : float;
function Start () { bulletShotSound = gameObject.GetComponent.(); targetToFollow = GameObject.FindGameObjectWithTag("Enemy").transform; numSwitch = 0; //mySwitch = false; shootSwitch = false; //speed = 3; }
///// COLLISION DETECTOR WHEN ENEMY IS IN RANGE function OnTriggerStay(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
// mySwitch = true;
shootSwitch = true;
numSwitch = 1;
}
}
//////// DETECTION WHEN NOT IN RANGE function OnTriggerExit(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
// mySwitch = false;
shootSwitch = false;
numSwitch = 0;
}
}
function Update () { ////// UPDATE ////////////
TimeContainer += Time.deltaTime;
///////// ENABLE FIRING if(shootSwitch == true) {
if(TimeContainer >= BulletInterval) {
bulletShotSound.Play(); //var clone : Rigidbody; var clone = Instantiate(projectile, Spawnpoint.position, projectile.rotation); clone.velocity = Spawnpoint.TransformDirection (Vector3.forward*BulletSpeed); Destroy(clone,1.0f); TimeContainer = 0; }
}
////// FOLLOW ENEMY WHILE NOT IN RANGE if(numSwitch == 0) {
transform.LookAt(targetToFollow); transform.position += transform.forward FollowSpeed Time.deltaTime; }
} ////// UPDATE ///////////
The game is a battle simulator type, each player will look for enemy and destroy it, but after destroying 1 enemy, it does not kill the rest,
And also after the 1st enemy destroyed it keep on attacking on the same object that was already destroyed
var FollowSpeed : float ; private var mySwitch : boolean; private var shootSwitch : boolean; private var targetToFollow : Transform; private var numSwitch : float;
private var bulletShotSound : AudioSource;
var projectile : Rigidbody; var BulletSpeed : float; var BulletInterval :float; var Spawnpoint : Transform;
private var TimeContainer : float;
function Start () { bulletShotSound = gameObject.GetComponent.(); targetToFollow = GameObject.FindGameObjectWithTag("Enemy").transform; numSwitch = 0; shootSwitch = false;
}
///// COLLISION DETECTOR WHEN ENE$$anonymous$$Y IS IN RANGE function OnTriggerStay(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
shootSwitch = true;
numSwitch = 1;
}
}
//DETECTION WHEN NOT IN RANGE function OnTriggerExit(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
shootSwitch = false;
numSwitch = 0;
}
}
function Update () {
TimeContainer += Time.deltaTime;
//ENABLE FIRING if(shootSwitch == true) {
if(TimeContainer >= BulletInterval) {
bulletShotSound.Play();
var clone = Instantiate(projectile, Spawnpoint.position, projectile.rotation); clone.velocity = Spawnpoint.TransformDirection (Vector3.forward*BulletSpeed); Destroy(clone,1.0f); TimeContainer = 0; }
}
//FOLLOW ENE$$anonymous$$Y WHILE NOT IN RANGE if(numSwitch == 0) {
transform.LookAt(targetToFollow); transform.position += transform.forward FollowSpeed Time.deltaTime; }
}
Answer by andyblueAMS001 · Jul 21, 2020 at 05:00 PM
Oh okay i will try, i dont know why it happend like this when i copy paste my code here, im new here i will try to post again thanks
var FollowSpeed : float ; private var mySwitch : boolean; private var shootSwitch : boolean; private var targetToFollow : Transform; private var numSwitch : float;
private var bulletShotSound : AudioSource;
var projectile : Rigidbody; var BulletSpeed : float; var BulletInterval :float; var Spawnpoint : Transform;
private var TimeContainer : float;
function Start () { bulletShotSound = gameObject.GetComponent.(); targetToFollow = GameObject.FindGameObjectWithTag("Enemy").transform; numSwitch = 0; shootSwitch = false;
}
function OnTriggerStay(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
shootSwitch = true;
numSwitch = 1;
}
}
//DETECTION WHEN NOT IN RANGE
function OnTriggerExit(col : Collider) {
if(col.gameObject.CompareTag("Enemy"))
{
shootSwitch = false;
numSwitch = 0;
}
}
function Update () {
TimeContainer += Time.deltaTime;
//ENABLE FIRING
if(shootSwitch == true) {
if(TimeContainer >= BulletInterval) {
bulletShotSound.Play();
var clone = Instantiate(projectile, Spawnpoint.position, projectile.rotation); clone.velocity = Spawnpoint.TransformDirection (Vector3.forward*BulletSpeed); Destroy(clone,1.0f); TimeContainer = 0; }
}
//FOLLOW ENE$$anonymous$$Y WHILE NOT IN RANGE
if(numSwitch == 0)
{
transform.LookAt(targetToFollow); transform.position += transform.forward FollowSpeed Time.deltaTime; }
}
i dont know why it does not format properly, i tried but it does not format good
Your answer
Follow this Question
Related Questions
How can i destroy a gameobject? 2 Answers
Script doesn't work anymore! 2 Answers
Destroy Objects after a set time 2 Answers
how to destroy all gameobjects with a certain tag? 1 Answer
Destroying Assests Not Permitted 1 Answer