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 camtronius · May 06, 2014 at 04:25 AM · vector3listpathfindingfor-looprts

How to detect multiple objects for avoidance

Hi Everyone, I have three 3D objects that move with A*pathing. I am testing some local avoidance code that is basically supposed to repel units from each other. The code detects surrounding units from a list and if they are too close it pushes them away. It works for two of the objects but not the other. Could anyone tell me why this doesnt work? I have the code im using below and a screenshot of my display here: http://imgur.com/2rkCAjg (code works for the bottom 2 units in the picture)


 public void LocalAvoidance(){
         //get drawn ray between all units
         for (int i=0; i<UnitsList.Count-1; i++) { //this iterates through my list of unit objects
         for(int y=0; y<UnitsList.Count; y++){ //this iterates again through the list to make sure we capture all objects
         
             Debug.DrawRay (UnitsList[i].transform.position, UnitsList[y].transform.position - UnitsList[i].transform.position);
                     //this draws a ray from unit to unit
         
             float distance = Vector3.Distance (UnitsList[i].transform.position, UnitsList[y].transform.position);
                     //this calculates the distance from one unit to another

             if (distance <= 3) { //if the distance is less than 3 push the unit away

                 Vector3 direction = Vector3.zero; //creates zero vector

                 direction=UnitsList[i].transform.position - UnitsList[y].transform.position; //gets the direction vector

                 transform.Translate(-direction*6*Time.deltaTime); //pushing in the opposite direction of the movement.
             }

         }

         }

 }
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 camtronius · May 06, 2014 at 06:04 PM

Im thinking that the solution may be making the first for loop without the -1 after UnitList. I think i was skipping an index because of this.

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

20 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

Related Questions

A node in a childnode? 1 Answer

How to add gameobject inside of an array? 0 Answers

Choose 10 items on each iteration 1 Answer

Need to find a point in a line using a percentage and a Vector3 list. 1 Answer

Boids/Flocking Tutorial 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