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 Yoshinator2 · Nov 27, 2016 at 12:17 AM · physicsphysics2ddistanceoverlapcalculation

Calculate distance and find nearest object

I'm making a 2D game and want to have the AI enemies find the nearest object with the tag "Friendly" or "Player". The objects with those tags will be constantly moving, so that needs to be taken in account. Heres what I have so far, I just need to figure out how to determine the nearest object, and then set its constant position as a target until it dies or something comes closer.

My code:

 private Rigidbody2D bruteRigidBody;
 private float test;
 private Vector2 center;
 public float radius;
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     center = transform.position;
     PathFinder(center, radius);
 }
 void PathFinder(Vector2 center, float radius)
 {
     Collider2D[] enemies = Physics2D.OverlapCircle(center, radius, friendlies);
     int i = 0;
     while (i < enemies.Length)
     {
         if(enemies[i].gameObject.tag == "Player")
         {
             test = Vector3.Distance(center, enemies[i].transform.position);
         }
         if(enemies[i].gameObject.tag == "Friendly")
         {

             test = Vector3.Distance(center, enemies[i].transform.position);
         }
         i++;
     }
 }

Important note: The line of code that is

Collider2D[] enemies = Physics2D.OverlapCircle(center, radius, friendlies);

is unfinished and I didn't really know what I was doing when I wrote it. The friendlies variable is a layer, and as I said I need it to find gameObjects with tags. I'm not sure how I could change this to find tags.

Comment
Add comment · Show 3
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 Nova-1504 · Nov 27, 2016 at 12:39 AM 0
Share

Not sure this is what you want, but If you want to find tags, use a bunch of Raycasts combined with if other.tag="friendly" or if other.tag="enemy".

avatar image Yoshinator2 Nova-1504 · Nov 27, 2016 at 03:56 AM 0
Share

Can you explain a little more? I'm confused on how a Raycast would help

avatar image Yoshinator2 Nova-1504 · Nov 27, 2016 at 04:07 AM 0
Share

And also, how would I calculate the distance between the characters, and find the closest one?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by OutOfRam · Nov 27, 2016 at 10:04 AM

https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

Check out the second example on this page, I think it is what you are looking for

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

111 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

Related Questions

Physics Movement without Physics Reactions 1 Answer

Physics2D, impersonate old RigidBody2D "Freeze Position" How? 1 Answer

Calculation errors? 0 Answers

How to make object with velocity bounce off the obstacles? 0 Answers

How can i get a transform to move to another transform if it is touching it? 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