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
1
Question by brendans95 · Feb 04, 2016 at 09:09 AM · c#transform.positiontag

Finding transform.position of Object with specific Tag

Heyo chaps

I've got my code for my 'enemy' working nicely. There will be lots of these enemies in 2D space and in order to prevent them overlapping each other I want them to move away when they're within a certain range. This all works using my current code but currently I manually need to plug in ONE target in the inspector and as such it will avoid this target only.

What I want is to replace the public Transform Target; with 'GameObject.FindWithTag'so I can just use a universal tag of 'enemy' and they will all avoid each other but I'm not 100% sure how to implement it and where to carry it through in the rest of my code - being a bit of a novice to programming.

Every method I've tried so far has led to errors throughout the entire code so any direction would be much appreciated!!

  using UnityEngine;
     using System.Collections;
     
     public class AvoidAllies : MonoBehaviour
     {
         public Transform Target;
         public float speed = 5f;
         private float DodgeDistance = 3f;
         private float range;
         public float dirNum;
     
         void Update()
         {
     
             range = Vector2.Distance(transform.position, Target.position);
     
             if (range < DodgeDistance)
             {
                 transform.position = Vector2.MoveTowards(transform.position, Target.position, -1 * speed * Time.deltaTime);
             }
         }
     }
 
 
Comment
Add comment · Show 4
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 npatch · Feb 04, 2016 at 10:41 AM 1
Share

There are ways to do this, but I think it will collapse easily. First of all, FindWithTag returns only one active object,what you need is FindGameObjectsWithTag which returns GameObject[], so you'd have to change Transform to GameObject[] and refactor the code to reflect that. Also Vector2.$$anonymous$$oveTowards as it is now will just try to move to the opposite direction if the distance is lower than a threshold. Even if you changed the above code to gather the rest of the enemies into an array and iterated through it, you'd probably hit some edge cases where you've checked for Enemy A who is towards direction A but some iterations later , the dodging gets you to move towards Enemy A again, and since you iterate and check through an array it won't help you.

You should move the code above into a $$anonymous$$anager of sorts who tries to calculate distances from everyone and try to figure out a direction which is valid for all other enemies. For example find the directions where an enemy is below the dodging distance and try to find a direction which takes into account all of the above directions.

avatar image brendans95 npatch · Feb 04, 2016 at 01:16 PM 0
Share

You make a fair point. Seems like there's no real easy way to get through this without encountering (as you said) a pretty detrimental collapse. I'm thinking I'll make a few different versions of the enemy, each with a different distance they have to keep from the main player. Even if it's just a small difference, it'll be enough to remove the jarring visual effect of all the enemies converging on a single point. Also the player will be destroying these enemies enough that it shouldn't be a major problem anyway.

Thanks for shedding some light!

avatar image npatch brendans95 · Feb 08, 2016 at 08:59 AM 0
Share

Whatever works for you, glad I helped

avatar image LazyElephant · Feb 16, 2016 at 03:31 AM 0
Share

If you haven't already done so, you may want to look into steering behaviours. You can find some information here http://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors--gamedev-12732 or with a quick google search of the topic.

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

74 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# transform position 1 Answer

Unity Camera and transform? 1 Answer

C# change an object tag wit raycasthit. 2 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