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 ThatUnityNoob · Jun 16, 2013 at 04:17 PM · aitargeting

AI Picking New Target After First Target Gets Destroyed

Hi! All I would like to know is how I could make my friendly AI pick a new target after it's original target was destroyed. When it's original target gets destroyed, the friendly AI's target comes up as "missing." I have been trying to target a new target (also the enemy) with gameobject.findwithtag but it's not working. So far all I have is when the enemy dies, it tells the friendly AIs that it's destroyed and they need to find a new target. Thanks!! P.S. - I'm trying to target the same enemy kind but different gameobject. Update with code of my friendly unit :P)

 var target : Transform; //the enemy's target
 var moveSpeed = 4; //move speed
 var rotationSpeed = 3; //speed of turning
 
 private var nextFireTime : float;
 var fireDelay : float;
 
 public var chasing = false; //Player
 var shootSound : AudioClip; 
 var Dead : GameObject;
 var hitPoints : int = 200;
 var Bullet : GameObject;
 var hitParticles : ParticleEmitter; //Sparks
 var hitParticles2 : ParticleEmitter; //Blood
 var hitParticles3 : ParticleEmitter; //Dirt
 var force = 50.0;
 var range = 150.0;
 var decalHitWall : GameObject;
 var BulletTracer : GameObject;
 var SpawnPoint : GameObject;
 private var floatInFrontOfWall : float = 0.001;
 var damagechest = Random.Range(10.0, 15.0);
 var hitdamage = Random.Range(70.0, 80.0);
 var myTransform : Transform; //current transform data of this enemy
 public var Fire : int = 0;
 var hitTime : float;
 private var nextHitTime : float;
 public var AtPosition = false;
 public var JustGotShot = false;
 public var targetdestroyed : boolean;
 
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance 
           
     }
 
 function Start()
 {
       target = GameObject.FindWithTag("Enemy").transform;
     }
 
 function Update () {
 if (hitPoints <= 0)
    {
       Destroy(gameObject);
       Instantiate(Dead, transform.position, Quaternion.identity);
       GameObject.Find("LZ").GetComponent(PointManager).points -= 100;
    }
    
    // check distance to target every frame:
 var distance = (target.position - myTransform.position).magnitude;
    
     if(JustGotShot == true) {
     GetComponent(FriendlyDodge).GetHim = true;
    }
 
     if (chasing) {
         //rotate to look at the player
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
         Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
         //move towards the player
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;;
          
  if (distance <= 10 && Fire == 1) {
        moveSpeed = -1;    
 }
  if (distance >= 11) {
        moveSpeed = 4;    
 }
  if(distance <= 4) {
   GetComponent(FriendlyRaycast).noMelee = 1;
   Fire = 0;
   if(Time.time > nextHitTime) {
   hitdamage = Random.Range(70.0, 80.0);
   GameObject.FindWithTag("Enemy").GetComponent(Enemy).TakeDamage(hitdamage);
   nextHitTime = Time.time + hitTime; 
   }
 }
  if(distance >= 5) {
  GetComponent(FriendlyRaycast).noMelee = 0;
 }
 }
 if(chasing == false && GetComponent(FriendlyDodge).Attack == true && AtPosition == false) {
         distance3 = (GetComponent(FriendlyRaycast).LastPlayerPosition - myTransform.position).magnitude;
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
         Quaternion.LookRotation(GetComponent(FriendlyRaycast).LastPlayerPosition - myTransform.position), rotationSpeed*Time.deltaTime);
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;;
         if(distance3 <= 0.5) { 
         AtPosition = true;
         chasing = true;
         GetComponent(FriendlyDodge).Attack = true;
         }
         }
         if(GetComponent(FriendlyRaycast).CantSee == true && AtPosition == true) {
         chasing = false;
         GetComponent(FriendlyDodge).Attack = false;
         }
         
         
 if(Time.time > nextFireTime && Fire == 1) {
         
         FireOneShot();        
         nextFireTime = Time.time + fireDelay; 
         audio.PlayOneShot(shootSound); 
         } 
         if(targetdestroyed == true) {
         
         FindNewTarget();    
         }
         }
         function FindNewTarget() {
         
         target = GameObject.FindWithTag("Enemy").transform;
         }
Comment
Add comment · Show 8
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 robertbu · Jun 16, 2013 at 04:51 PM 0
Share

We can be more helpful if you post your code. GameObject.FindWithTag() should work.

avatar image samtperrin · Jun 16, 2013 at 05:07 PM 0
Share

Code please!!

avatar image ThatUnityNoob · Jun 16, 2013 at 09:29 PM 0
Share

Updated..If you need some of the other code, just let me know :) Thanks.

avatar image samtperrin · Jun 17, 2013 at 10:36 AM 0
Share

Do all the enemies definitely have the correct tags?

avatar image ThatUnityNoob · Jun 17, 2013 at 12:59 PM 0
Share

Yes, all the enemy AI's have the tag "Enemy" and their chest has the tag "chest" for the friendly AI to detect it by raycast

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

How to determine a "cone of influence" used for targeting an object. 2 Answers

What would be the best way to setup Target priority 1 Answer

Hey I need an AI script for a character noticing nearby characters! 2 Answers

Targeting multiple tags 1 Answer

Enemy AI targeting script 3 Answers


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