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 /
avatar image
0
Question by Harardin · Mar 10, 2017 at 05:33 PM · c#vector3coroutineupdate

How to Update a GameObject coordinates to folow in realtime and don't lose performens or crushing Unity.[SOLVED]

I have a small issue and jus out of ideas how to do it, I want to update a GameObject Vector3 in realtime but if I use simple Update method fps drops low and it's understandable.

Ther is a many objects whit tag enemy in the scene and I want to make a main object to folow the closest and change the object to dolow if another "enemy" object became closer.

I also tryed to use System.Threading but it did't give me ane result fps lowers the same way.

Here is small excample of what I'am trying to achive:

     Vector3 target;
 
     private void Start()
     {
         StartCoroutine(UpdatePath());
     }
 
     private IEnumerator UpdatePath()
     {
         target = GameObject.FindGameObjectWithTag("Enemy").transform.position;
         StartCoroutine(FolowPath());
         yield return null;
     }
 
     private IEnumerator FolowPath()
     {
         while (true)
         {
             Vector3.MoveTowards(transform.position, target, 20 * Time.deltaTime);
             StartCoroutine(UpdatePath());
             yield return null;
         }
     }
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
1
Best Answer

Answer by IgorAherne · Mar 10, 2017 at 06:01 PM

Oh god,

UpdatePath starts FollowPath1,

FollowPath1 starts UpdatePath wich launches FolowPath2,

FollowPath1 continues its while loop, starts FollowPath3,

FollowPath2 launche UpdatePath, which launches FollowPath4,

Follow Path3 launches UpdatePath which launches FollowPath5,

Follow path5 launches UpdatePath which launches FollowPath6, etc

Soon you explode with an insane amount of FollowPaths calling their operations from never ending while loops, evolving in a geometric progression.

Do this instead:

 private void Start()
      {
          StartCoroutine(UpdateAndFollowPath());
      }
  
      private IEnumerator UpdateAndFollowPath()
      {
          while (true)
          {
              //move this like into here:
              target = GameObject.FindGameObjectWithTag("Enemy").transform.position;
              Vector3.MoveTowards(transform.position, target, 20 * Time.deltaTime);
              //UpdatePath());  remove this
              yield return null;
          }
      }





Comment
Add comment · Show 4 · 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 Harardin · Mar 10, 2017 at 06:10 PM 0
Share

I guess it my falt a multiple corotines is bad thing your advice increases fps, not perfect but better thanks.

avatar image tanoshimi · Mar 10, 2017 at 06:25 PM 1
Share

$$anonymous$$ultiple coroutines are not a bad thing. Infinitely recursive coroutines are, however, a (very) bad thing. Another bad thing is calling GameObject.FindGameObjectWithTag every frame. Find your enemy once on Start and cache the reference ins$$anonymous$$d:

 private Transform enemyTransform;
 private void Start()
   {
       enemyTransform = GameObject.FindGameObjectWithTag("Enemy").transform;
       StartCoroutine(UpdateAndFollowPath());
   }
  
   private IEnumerator UpdateAndFollowPath()
   {
       while (true)
       {
           //move this like into here:
           target = enemyTransform.position;
           Vector3.$$anonymous$$oveTowards(transform.position, target, 20 * Time.deltaTime);
           //UpdatePath());  remove this
           yield return null;
       }
   }
avatar image Harardin tanoshimi · Mar 11, 2017 at 10:40 AM 0
Share

I have many "clones" of the same object "enemy" that I Instantiated that why I have to use GameObject.FindGameObjectWithTag and I need always update the position of clones to find the closest one because they are always moving. and If I use main pref it will cause the modifying of all clones at ones.

$$anonymous$$aybe there is a better whay of doing this but I don't know how for now.

avatar image Bunny83 Harardin · Mar 11, 2017 at 10:59 AM 1
Share

You can cache it "intelligent" in that case:

 Transform enemyTransform;
 
 private IEnumerator UpdateAndFollowPath()
 {
     while (true)
     {
         if (enemyTransform == null)
             enemyTransform = GameObject.FindGameObjectWithTag("Enemy").transform;
         // [ ... ]

If you currently don't have an enemy, search for it. If it got destroyed it will search for a new one. In between it will reused the one stored in enemyTransform.

edit
Btw: GameObject.FindGameObjectWithTag will not return the closest one but simply any object that is tagged with that tag. Usually in the order they have been created. If you want to target the closest enemy you probably want to use Physics.OverlapSphere to get all objects around you in a certain radius.

If you have many enemies and it should target the closest everywhere in the scene you might want to use an "Enemy$$anonymous$$anager" where each enemy registers itself on creation and removes itself on destroy.

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

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

Code working on wrong axis. 1 Answer

Starting a Coroutine in Update acting differently in play mode and built game 1 Answer

Distribute terrain in zones 3 Answers

Using multiple yields in a Coroutine 1 Answer

Multiple Cars not working 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