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 kamuzai1226 · Nov 29, 2014 at 03:53 AM · collidercoroutinephysics2dtargeting

Coroutine not working correctly?

I am trying to get the closest target to the unit using Physics2D.OverlapCircleAll, but I do not think that the coroutine is actually running and I don't know how to fix it. Please help.

 using UnityEngine;
 using System.Collections;
 
 public class Target : MonoBehaviour
 {
 
     public LayerMask layer;
     public float targetRange = 2f;
     public Transform target;
 
     public float currentDist;
 
     public Transform currentTarget;
 
     public Collider2D[] cols;
 
     private RaycastHit2D hit;
 
     private Vector2 thisPos;
 
     // Use this for initialization
     void Start ()
     {
         StartCoroutine(ScanForTargetAllAround());
     }
     
     // Update is called once per frame
     void Update ()
     {
         thisPos = new Vector2(transform.position.x, transform.position.y);
 
         cols = Physics2D.OverlapCircleAll(thisPos, targetRange, 1 << LayerMask.NameToLayer("Unit"));
 
         if (!target)
         {
             hit = Physics2D.Raycast(transform.position, Vector2.up);
             Debug.DrawRay(transform.position, Vector2.up);
         }
 
         if (target)
         {
             var direction = target.position - transform.position;
 
             hit = Physics2D.Raycast(transform.position, direction);
             Debug.DrawRay(transform.position, direction);
 
             float angle = Mathf.Atan2(direction.y, direction.x)*Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         }
     }
 
     IEnumerator ScanForTargetAllAround()
     {
         while (cols.Length > 1)
         {
             currentTarget = null;
 
             float distance = Mathf.Infinity;
 
             Collider2D currentCollider2D = cols[0];
 
             for (int i = 0; i < cols.Length; i++)
             {
                 currentDist = Vector2.Distance(thisPos, cols[i].transform.position);
 
                 if (currentDist < distance)
                 {
                     currentCollider2D = cols[i];
 
                     distance = currentDist;
                 }
             }
 
             currentTarget = currentCollider2D.gameObject.transform;
     
             target = currentTarget;
 
 
         }
         
         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
0

Answer by yashpal · Nov 29, 2014 at 04:42 AM

@kamuzai1226, I think you wan to call ScanForTargetAllAround Coroutine many times. but here you just call StartCoroutine(ScanForTargetAllAround()) in Start so it called only one time. so put it on Update() or inside Coroutine so it called itself when it complete.

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 kamuzai1226 · Nov 29, 2014 at 07:20 AM 0
Share

But do you think this would be okay with 500 of these units at once? $$anonymous$$y understanding of the coroutine is that it keeps running the method, no?

avatar image yashpal · Nov 29, 2014 at 11:02 AM 0
Share

@kamuzai1226, coroutine is not running infinite. acoording to documentation are use for fade like things. So you don't need to make coroutine you can use function and called form Update() in your script. Here is a basic use of coroutine. Feel free to comment if you have question.

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

27 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

Related Questions

OverlapCircle funcion to avoid Gameobject creation if Collision ? 1 Answer

Ball bounces in the wrong direction 1 Answer

How do I make bullets pass through some game objects? 1 Answer

Verification Errors on creating a PolygonCollider2D from a contour trace 1 Answer

Unity 5: RigidBody or No RigidBody? 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