Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by hvd · Dec 03, 2015 at 11:02 PM · enemy aiupdate functionfollow player

Enemys stop following after destroying gameobject

Hi guys, i have this Enemy Script where my enemys should follow my playerparts(snakelike player) the segments get instantiated while playing so i have to get a reference to it in 'update function' i quess?! If the enemys are in range they attack the player parts and destroy them. BUT NOW if they had destroyed one part they just keep standing and not following anymore. I think they are stuck on the old reference of the last part they worked on so how can i let them search for a new part to follow?! Also i have the problem if there more then one part of the same type they just following the first one, is there maybe an easy solution? I just figured out to make an array of all segment positions and look what range is the shortest but im not really sure how to work that out right now.

 Transform Head;
 Transform segpos1;
 Transform segpos2;
 Transform segpos3;
 Transform segpos4;
 
        float speed = 1.8f;
        float minDistance = 0.5f;
        float rangehead;
        float rangeseg1 = 100;
        float rangeseg2 = 100;
        float rangeseg3 = 100;
        float rangeseg4 = 100;
 
  void Start()
     {   
         Head = GameObject.FindGameObjectWithTag("Head").transform;
             
     }
 
  void Update()
     {
 
         if (Head != null)
         {
             rangehead = Vector2.Distance(transform.position, Head.position);
 
             if (rangehead < rangeseg1 && rangehead < rangeseg2 && rangehead < rangeseg3 && rangehead < rangeseg4)
             {
                 if (rangehead > minDistance)
                 {
                     transform.position = Vector2.MoveTowards(transform.position, Head.position, speed * Time.deltaTime);
                 }
             }
         }
 
         if (segpos1) { }
         else
         {
             if (GameObject.FindWithTag("seg1"))
             {
                 segpos1 = GameObject.FindWithTag("seg1").transform;
             }
         }
 
         if (segpos1 != null)
         {
             rangeseg1 = Vector2.Distance(transform.position, segpos1.position);
 
             if (rangeseg1 < rangehead && rangeseg1 < rangeseg2 && rangeseg1 < rangeseg3 && rangeseg1 < rangeseg4)
             {
                 if (rangeseg1 > minDistance )
                 {
                     transform.position = Vector2.MoveTowards(transform.position, segpos1.position, speed * Time.deltaTime);
                 }
 
                 
             }
         }
 
         if (segpos2) { }
         else
         {
             if (GameObject.FindWithTag("seg2").transform)
             {
                 segpos2 = GameObject.FindWithTag("seg2").transform;
             }
         }
 
         if (segpos2 != null)          
         {
         rangeseg2 = Vector2.Distance(transform.position, segpos2.position);
 
         if (rangeseg2 < rangehead && rangeseg2 < rangeseg3 && rangeseg2 < rangeseg4 && rangeseg2 < rangeseg1)
         {
             if (rangeseg2 > minDistance )
             {
                 transform.position = Vector2.MoveTowards(transform.position, segpos2.position, speed * Time.deltaTime);
             }
             
         }
         }
 
         if (segpos3) { }
         else
         {
             if (GameObject.FindWithTag("seg3").transform)
             {
                 segpos3 = GameObject.FindWithTag("seg3").transform;
             }
         }
 
         if (segpos3 != null)   
         {
         rangeseg3 = Vector2.Distance(transform.position, segpos3.position);
 
         if (rangeseg3 < rangehead && rangeseg3 < rangeseg2 && rangeseg3 < rangeseg1 && rangeseg3 < rangeseg4)
         {
             if (rangeseg3 > minDistance)
             {
                 transform.position = Vector2.MoveTowards(transform.position, segpos3.position, speed * Time.deltaTime);
             }
             
         }
         }
 
         if (segpos4) { }
         else
         {
             if (GameObject.FindWithTag("seg4").transform)
             {
                 segpos4 = GameObject.FindWithTag("seg4").transform;
             }
         }
 
         if (segpos4 != null) 
         {
             rangeseg4 = Vector2.Distance(transform.position, segpos4.position);
 
             if (rangeseg4 < rangeseg1 && rangeseg4 < rangeseg2 && rangeseg4 < rangeseg3 && rangeseg4 < rangehead)
             {
                 if (rangeseg4 > minDistance)
                 {
                     transform.position = Vector2.MoveTowards(transform.position, segpos4.position, speed * Time.deltaTime);
                 }
                 
             } 
         }
 
         if (segpos1 = null)
         {rangeseg1 = 100f; }
         else if (segpos2 = null)
         { rangeseg2 = 100f; }
         else if (segpos3 = null)
         { rangeseg3 = 100f; }
         else if (segpos4 = null)
         { rangeseg4 = 100f; }
 
     }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hvd · Dec 10, 2015 at 02:21 AM

I already had the wright solution but made a little mistake: in the last if statements i set the segments to null instead of check if its null ^^ ...

Comment
Add comment · 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

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

33 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

Related Questions

Enemy follow player 0 Answers

Enemy AI chase, then attack player 1 Answer

How to evade Enemy AI? 1 Answer

Enemy doesn't follow player clone,Enemy doesn't follow the player clone 0 Answers

how to make the random enemy generation with in the orthographic view of camera? 0 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