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
0
Question by munggol97 · Jun 05, 2019 at 06:25 AM · collisionrigidbody2dplayer movementvector2movetowards

Put an offset distance to gameobject that follows player

I have these multiple gameobjects that the player can pick-up and starts following him like its pets but what's happening is when the player picks up more than one of this game object is starts stacking/overlapping its position so it looks like there is only 1 following the player. How can I prevent this gameobjects from overlapping their position while following the player? I attached a sketch of to make it clear, also the code that I use to follow the player

public float radius; public float speed = 3; public float pickDistance = 1.5f;

 void Update()
 {
     Collider2D[] colls = Physics2D.OverlapCircleAll(transform.position, radius);

     foreach (Collider2D c in colls)
     {
         if (c.tag == "Player")
         {
             if (Vector2.Distance(transform.position, c.transform.position) > pickDistance)
             {
                 transform.position = Vector2.MoveTowards(transform.position, c.transform.position, speed * Time.deltaTime);
             }
                
         }
        
     } 
 }


alt text

sketch.png (16.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Jun 05, 2019 at 07:43 AM

As your player picks up these pets, put them in a list. Then iterate through the list and add the offset distance * (index + 1).


Make your list:

 List<Transform> pets = new List<Transform>();



Add to your list:

 pets.Add(transform);



When you want to set their offset, loop through them like so:


 float xOffset = 1.5f;
 
 for(int i = 0; i < pets.Count; i++)
 {
 pets[i].position = new Vector2(player.transform.position.x + (xOffset * (i + 1)), player.transform.position.y);
 }

Untested, but that should do it. If you want an offset on the y axis, you can add an additional offset float for that.


Additionally, you can add the offset when you add a new pet to the list:

 pets.Add();
 pets[pets.Count - 1].position = new Vector2(player.transform.position.x + (xOffset * pets.Count), player.transform.position.y);



Then making the player transform a parent will keep them at the offset.


Cheers

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
avatar image
0

Answer by DCordoba · Jun 05, 2019 at 07:54 AM

that seem to you're using a independent, modular, less-interconected pet AI, thats great to deploy on any scene, and they are able to reroute themselves, so, good approach.

the problem is, you need to make the AI detect each other, so... can be a problem because they need to know the position of the rest of the swarm on each update, to take decision to advance, so you need to have a way to each AI detect a pal, and make a following formation.

to a line pattern of persecution you can manage easy on several ways

you can do a pack-like way, when a object starts following, it send a "howl", as answer receive the number on the line to it have to align and it automatically calculates the offset, some like:

 public static class PackSharedMind{
       static int packsize = 0;

   public static int Howl(){
     packsize++;
     return packsize - 1;
   } 
  }

and inside the Update of each element

  public float radius; 
  public float speed = 3; 
  public float pickDistance = 1.5f;
  public float offset;
 int packRank = 0;//ranks on the pack are inverse, 0 means first (leader of dogs, wolfs, etc xd) so go ahead
 bool following = false;
 void Update()
 {
     Collider2D[] colls = Physics2D.OverlapCircleAll(transform.position, radius);
     foreach (Collider2D c in colls)
     {
        if (c.tag == "Player")
        {
            if (!following){
                packRank = PackSharedMind.Howl();
                following = true;
            }
            if (Vector2.Distance(transform.position, c.transform.position) > pickDistance + packRank*offset)
            {
                transform.position = Vector2.MoveTowards(transform.position, c.transform.position, speed * Time.deltaTime);
             }
        } 
     }
 }

EDIT: I was writing while @highpockets write a answer, after read his answer I realized that the pet never get the the oportunity of leave, so, if stops following, you'll have holes on your formation, using his approach, I suggest make a most powerful PackSharedMind, able to give feedback and reorder the formation if someone is off the radius and leave

  public static class PackSharedMind{
     //I dont know the name of your AI scrip, replace "Pet" to the right name... 
 static List<Pet> pack = new List<Pet>();
 /// <summary>
 /// add one element to the followers.
 /// </summary>
 public static int Howl(Pet newMember){
     pack.Add(newMember);
     return pack.Count - 1;
 }
     public static void Leave(int rank){
           pack.RemoveAt(rank);
           for (int i = rank; i<pack.Count(); i++)
                  pack(i).Reroute(i); 
     }

 }

and inside Pet update

  public float radius; 
  public float speed = 3; 
  public float pickDistance = 1.5f;
  public float offset;
 int packRank = 0;//ranks on the pack are inverse, 0 means first (leader of dogs, wolfs, etc xd) so go ahead
 bool following = false;
 
 //check if this instance should stop following 
 bool found = false;
 void Update()
 {
     Collider2D[] colls = Physics2D.OverlapCircleAll(transform.position, radius);
     found = false;
     foreach (Collider2D c in colls)
     {
        if (c.tag == "Player")
        {
            if (!following){
                packRank = PackSharedMind.Howl();
                following = true;
            }
            if (Vector2.Distance(transform.position, c.transform.position) > pickDistance + packRank*offset)
            {
                transform.position = Vector2.MoveTowards(transform.position, c.transform.position, speed * Time.deltaTime);
             }
             found = true;
        } 
     }
     if(following && !found){
           PackSharedMind.Leave(packRank);
           following = false;
     }
 }

also inside "Pet" add method Reroute() to receive a new position from the pack shared mind when someone else leaves:

  public void Reroute(int newRank){
        PackRank = newRank;
  } 
      
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

109 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

Related Questions

Am I using MoveTowards and MovePosition wrong? 1 Answer

Can you override the physics behaviour for a particular object? 1 Answer

How to store variables in start method 1 Answer

Is there an easy way to find out exactly where a game object was hit in a collision (top, bottom, upper left, etc.)? 0 Answers

Make object jump to fixed y position 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