• 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 AlexVlad · Jul 04, 2018 at 10:40 AM · followwaypoint

Make 2d Balls follow one another

Hi guys, I am trying to instantiate a row of balls. I want to instantiate them one after the other and launch them in the same direction and make them follow one another. the idea is than when the first one bounces of a wall the others should follow its trajectory, so the same path but with a slight delay. I can't seem to get it working.

I apply a force to the first one, deactivate the others' rigidbodies and try to make them follow it or the one in front of each one but i can't get it to work. launching them with rigidbodies in the same direction but with a delay almost works, but if they encounter moving objects they each bounce off in a particular direction. I need them to move in a single file.

Does anyone have an idea, please?? I am not asking for code necessarily, but at least an idea or a logic.

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
1
Best Answer

Answer by Vincent1236 · Jul 04, 2018 at 11:09 AM

This can be done in a few ways depending on exactly how you want it. One way would be give each ball a reference to the ball it is behind. And then have each ball lerp/interpolate to the location of the ball in-front of it. Something like this:

 public Transform BallToFollow;
 public float LerpScale=1f;
 void Update()
 {
     transform.position = Vector3.Lerp(transform.position, BallToFollow.position, LerpScale * Time.deltaTime);
 }

Where you will need to just pass in to each ball the ball it should be following. So the ball in front won't have this script, ball 2 will have a reference to the ball in front, 3 will have a reference to 2 etc.. Then disable the rigidbodies on all the trail balls, they shouldn't even need them

If you need a solution that perfectly follows the balls in front, take a look at Casper-Chimp's answer, it uses the same principle, but instead of lerping uses queues to match the leading ball''s location perfectly!

Comment
Add comment · Show 2 · 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 AlexVlad · Jul 04, 2018 at 11:43 AM 0
Share

Hey, thanks for the quick reply. Yes, your answer has put me on the right track. I am currently having an issue when the first ball bounces off the wall, the second one should look like it is also bouncing off at the same contact point, but due to the lerping it is turning towards the first ball faster. I will have to tweak some values, like the first ball's speed, the frequency with which I am instantiating the other balls and the LerpScale.

Nevertheless, your answer helped me see the problem in a different light. Will mark it as 'Accepted'. Thanks a ton!

avatar image Vincent1236 AlexVlad · Jul 04, 2018 at 03:28 PM 0
Share

Glad I could help get you on track. Definitely check out Casper-Chimp's answer, as it is one of the other ways I thought of doing it but thought it would be more work than is warranted. And as I said, there are a few ways and depending on how you want it it can be implemented differently.

But seeing as you are encountering an issue, where balls not looking like they are bouncing off the same location etc, that will remain a problem using the method I posted. His answer is quite short and well written, and it will definitely solve the issue you have currently!

avatar image
1

Answer by Casper-Chimp · Jul 04, 2018 at 12:58 PM

To follow the exact path of the leading object you can store the positions in a queu, where the size of the queu determines the distance behind the object. You can use it like the other script to chain objects together so each object just follows the one in front of it.

     public GameObject objectToFollow;
     public int stepsBehind;
     private Queue<Vector3> positions;
     // Use this for initialization
     void Start () {
         positions = new Queue<Vector3>();
         for(int i=0;i<stepsBehind;i++)
         {
             positions.Enqueue(transform.position);
         }
     }
     void FixedUpdate () {
         positions.Enqueue(objectToFollow.transform.position);
         transform.position = positions.Dequeue();
     }
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 Vincent1236 · Jul 04, 2018 at 03:30 PM 0
Share

Love your answer! Thought of something like this using queues, but didn't cross my $$anonymous$$d to do it in so few lines! Nice answer, added a comment to my answer saying that it should check yours out.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

87 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

Related Questions

Camera rotation around player while following. 6 Answers

The player's car following a certain path 1 Answer

How to make characters follow a path? 2 Answers

An object to follow another object through the waypoint - problem: taking a shortest path instead of walking through the waypoint. 1 Answer

Follow a dynamic path? 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