Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andyblueAMS001 · Jul 20, 2020 at 10:04 AM · followtagsdestroy objectdestroygameobject

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 ///////////

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andyblueAMS001 · Jul 19, 2020 at 01:34 PM 0
Share

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,

avatar image andyblueAMS001 · Jul 19, 2020 at 01:55 PM 0
Share

And also after the 1st enemy destroyed it keep on attacking on the same object that was already destroyed

avatar image rh_galaxy · Jul 20, 2020 at 09:18 PM 0
Share

You need to fix your code formatting so it's readable.

avatar image andyblueAMS001 · Jul 21, 2020 at 05:07 PM 0
Share

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; }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andyblueAMS001 · Jul 21, 2020 at 05:14 PM 0
Share

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; }

 }
avatar image andyblueAMS001 · Jul 21, 2020 at 05:15 PM 0
Share

i dont know why it does not format properly, i tried but it does not format good

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges