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 09, 2014 at 04:33 AM · collisionobjectvector3directionrts

Units merge into each other with pathfinding

Hey Guys, Its a 3d rts and I have posted the code below. The code gets a list of all the units on the map and draws a ray between them. It then calculates the distance between each unit. When the unit distance is less than 1.5 it repels the units by the vector given by the difference in their direction. Sometimes when three units are grouped and traveling together two of the units will merge together and i dont know why. I tihnk it has to do with opposing forces and them being too close to each other. Let me know if you guys have any ideas or suggestions! Thanks!!

 public void LocalAvoidance(){ //do the distance of specific vectors like x,z to determine direction unit is pushed back
     //get drawn ray between all units
     for (int i=0; i<UnitsList.Count; 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 <= 1.5) { //if the distance is less than 1.5 push the unit away

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

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

                 direction.y=0;//try to make it so units dont bounce up

                 transform.Translate(-direction*5*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

Answer by morethan3 · May 09, 2014 at 08:38 AM

What I think you what to do is move each object away from the average position of all near objects. alt text

So you would move it in the direction of the green arrow.

However, for a scenario like this:

alt text

You probably don't want to move the blue object, just all of the red ones.

Try the function below and see if that fixes it:

 public void LocalAvoidance(){ //do the distance of specific vectors like x,z to determine direction unit is pushed back
     //get drawn ray between all units
     for (int i=0; i<UnitsList.Count; i++) { //this iterates through my list of unit objects
         List<Vector3> nearUnits = new List<Vector3>();
         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
  
             Vector3 offset = UnitsList[y].transform.position - UnitsList[i].transform.position;
             float sqrLen = offset.sqrMagnitude;
             //2.25 is 1.5^2
             // Comparing distances this way is faster than using the distance function.
             if (sqrLen <= 2.25) { //if the distance is less than 1.5 push the unit away
                 nearUnits.add(UnitsList[y].transform.position);
             }
         }
         // Calculate the average position of nearby units here
         Vector3 tempPos = Vector3.zero;
         foreach(Vector3 unit in nearUnits) {
             tempPos += unit;
         }
         tempPos /= nearUnits.Length;
         Vector3 offset = tempPos - UnitsList[i].transform.position;
         //If you do want to avoid scenrio 2 show above do the following
         //otherwise just use the code inside the if statement
         float sqrLen = offset.sqrMagnitude;
         // I've just used a distance of 0.2 here you might want to play with this
         if (sqrLen > 0.2 * 0.2) {
             offset.y = 0; //try to make it so units dont bounce up
             transform.Translate(offset * 5 * Time.deltaTime); // It shouldn't need to be minus since I've already calculated the oppsite vector.
         }
     }
 }

vector1.png (2.8 kB)
vector1.png (2.0 kB)
Comment
Add comment · Show 1 · 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 camtronius · May 09, 2014 at 05:44 PM 0
Share

@morethan3 I found the answer to my question. I just needed to call the list index for my translation, UnitsList[i].transform.translate.etc, but i didnt know the square of the magnitude is faster than the distance. Your code may be useful then and I will try to implement to see what happens. Thanks for helping me man!

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

23 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

Related Questions

Make a moving object continue it's movement from the same direction , even it reaches target 1 Answer

Fire Knockback 3 Answers

Vector direction after collision 1 Answer

Slerp look at direction not working... 3 Answers

Get rotation from a vector direction. 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