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 Leia · May 17, 2018 at 10:47 PM · movementmultiple objectspathfollowingsquarespacing

How to make multiple, evenly-spaced objects follow any path with any number of points?

Hello! Somewhat of a n00b here...

I have been trying for several hours to write a piece of code that allows several objects to follow a square path. As they travel, these objects should: - Be spaced evenly - Not overlap - Leave gaps of the same size in between them

Though I have objects that follow a path, these objects cluster at the corners before moving on, resulting in gaps and unevenness.

I'd greatly appreciate it if anyone has a solution! And thank you kindly for your time :)

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

Answer by Leia · May 19, 2018 at 09:45 AM

Thanks so much to @ImpOfThePerverse - your script is genius!!

You saved my a lot of time and stress :)

Also, I'm terribly sorry for the late reply - but with a lot of help, the script has also been modified so that it'll work with any number of points with any amount of distance between them:

 // Child objects of this GameObject describe the path to follow
     public GameObject path;
 
     // How long a single object should take to complete a circuit
     public float circuitTime;

     private List <GameObject> _objects;
     private List <Vector3> _points;
     private List <float> _lengths;
     private float _pathLength;
     private float _timeMain;
 
     private float AddSegment (Vector3 from, Vector3 to)
     {
         float length = Vector3.Distance (from, to);
 
         _lengths.Add (length);
 
         return length;
     }
 
     void Start ()
     {
         float pathLength = 0.0f;
         Vector3? lastPoint = null;
 
         _timeMain = 0.0f;
 
         _points = new List<Vector3> ();
         _lengths = new List<float> ();
         foreach (Transform child in path.transform) {
             Vector3 point = child.gameObject.transform.position;
 
             _points.Add (point);
 
             if (lastPoint != null) {
                 pathLength += AddSegment (lastPoint.Value, point);
             }
 
             lastPoint = point;
         }
         pathLength += AddSegment (lastPoint.Value, _points [0]);
         _pathLength = pathLength;
 
         _objects = new List<GameObject> ();
         foreach (Transform child in transform) {
             _objects.Add (child.gameObject);
         }
     }
 
     void Update ()
     {
         float timeSpacing = circuitTime / _objects.Count;
 
         _timeMain += Time.deltaTime;
         if (_timeMain > circuitTime) {
             _timeMain -= circuitTime;
         }
 
         for (int i = 0; i < _objects.Count; i++) {
             if (_objects[i]!=null&&_objects [i].GetComponent<SpriteRenderer>().enabled) {
                 PositionObject (_objects [i], _timeMain + i * timeSpacing);
             }
         }
     }
 
     private int FindSegment (float distance)
     {
         int segment = 0;
         foreach (float length in _lengths) {
             distance -= length;
             if (distance < 0.0f) {
                 return segment;
             }
 
             segment += 1;
         }
 
         // Should never reach here
         return -1;
     }
 
     private float DistanceToSegment (int segment)
     {
         float total = 0.0f;
 
         for (int i = 0; i < segment; i++)
             total += _lengths [i];
 
         return total;
     }
 
     void PositionObject (GameObject gObject, float t)
     {
         int segment;
         float distance, partialDistance;
         Vector3 pointA, pointB;
 
         if (t > circuitTime) {
             t -= circuitTime;
         }
 
         distance = _pathLength * (t / circuitTime);
         segment = FindSegment (distance);
         pointA = _points [segment];
         if (segment < _points.Count - 1) {
             pointB = _points [segment + 1];
         } else {
             pointB = _points [0];
         }
 
         partialDistance = distance - DistanceToSegment (segment);
         gObject.transform.position = Vector3.Lerp (pointA, pointB, partialDistance / _lengths [segment]);
     }
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 ImpOfThePerverse · May 17, 2018 at 11:47 PM

If you're setting the object positions directly, you could try doing so from a single script that uses Vector3.Lerp(corner1, corner2, t). The advantage there is that you can have one t value that you subtract a fixed amount from to calculate the t values for the other objects, keeping them in lockstep. The following might work (I haven't tested it.) corner0-3 would be the corners of the path, circuitTime is the number of seconds to go all the way around the path, timeSpacing is the number of seconds between objects, and objects is an array of the objects.

 public GameObject corner0;
 public GameObject corner1;
 public GameObject corner2;
 public GameObject corner3;
 
 public float circuitTime;
 public float timeSpacing;
 
 public GameObject[] objects;
 
 float tMain;
 
 void Update()
 {
     tMain += Time.deltaTime;
     if (tMain > circuitTime)
     {
         tMain -= circuitTime;
     }
     for (int i = 0; i < objects.Length; i++)
     {
         PositionObject(objects[i], tMain - i * timeSpacing);
     }
 }
 
 void PositionObject(GameObject nextObject, float t)
 {
     Vector3 cornerA;
     Vector3 cornerB;
     float sideTime = circuitTime / 4f;
     if (t < 0f)
     {
         t += circuitTime;
     }
     if (t < circuitTime * 0.25f)
     {
         cornerA = corner0.transform.position;
         cornerB = corner1.transform.position;
     }
     else if (t < circuitTime * 0.5f)
     {
         cornerA = corner1.transform.position;
         cornerB = corner2.transform.position;
         t -= circuitTime * 0.25f;
     }
     else if (t < circuitTime * 0.75f)
     {
         cornerA = corner2.transform.position;
         cornerB = corner3.transform.position;
         t -= circuitTime * 0.5f;
     }
     else
     {
         cornerA = corner3.transform.position;
         cornerB = corner0.transform.position;
         t -= circuitTime * 0.75f;
     }
     t /= sideTime;
     nextObject.transform.position = Vector3.Lerp(cornerA, cornerB, t);
 }
 





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 Leia · May 19, 2018 at 09:50 AM 0
Share

Thank you so much - your script is genius!!

You saved my a lot of time and stress :)

Also, I'm terribly sorry for the late reply - but with a lot of help, the script has also been modified so that it'll work with any number of points with any amount of distance between them (see my script below).

(Also, I apologise for the duplicated message and comment; it would appear that I am also a n00b at using Unity Forums)

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

128 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 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

Movement and animation 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

How to move along a few path follow ? 1 Answer

MoveTowards on paths, different speeds 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