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 AmasterAmaster · Jul 02, 2015 at 10:01 PM · unity 5gameobjectpositionobjectmath

How to segment a number line of game objects between two points dynamically?

How can I dynamically segment a line between two points? For example, I want to put GameObjects between two points and as more objects get added, I want to segment the conceptual line by a number so I can have a multiple to separate the objects evenly. How would I go about it? Here is the code I was working with:

 float distance = 0;
 int numOfObjects = 1;
 float segment = 0;
 
 //Get the distance between two limits (left and right)
 distance = Vector3.Distance(leftSide.transform.position, rightSide.transform.position);
 //segment = leftSide.transform.position.x + rightSide.transform.position.x / numOfObjects;
 numOfObjects = numOfObjects + 1;
 float dist = Mathf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance / numOfObjects);
 //float dist = Mathf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance);
 
 //Set the position
 someObject.transform.position = new Vector3(dist * numOfObjects, rightSide.transform.position.y, rightSide.transform.position.z);


I know that this is more of a math question, but any help would be appreciated.

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 kingcoyote · Jul 03, 2015 at 12:05 AM

You probably want to use Vector3.Lerp rather than Mathf.InverseLerp.

 someObject.transform.position = Vector3.Lerp(
     leftSide.transform.position, 
     rightSide.transform.position, 
     thisSegment / (segments + 1)
 );
Comment
Add comment · Show 6 · 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 AmasterAmaster · Jul 03, 2015 at 07:00 AM 0
Share

Ok, I was playing around with the code above and trying to make sense of it. It sort of works, but only for a debug thing I created...

         //Draw a test cube
         Gizmos.color = Color.red;
 
         segmentDebug = amountDebug;
         if(segmentDebug == 1)
         {
             segmentDebug++;
         }
 
         offsetDebug = distanceDebug / segmentDebug;
         distanceDebug = Vector3.Distance(leftSide.transform.position, rightSide.transform.position);
         for(int i = 0; i < amountDebug; i++)
         {
             Gizmos.DrawCube(Vector3.Lerp(new Vector3(leftSide.transform.position.x + offsetDebug, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, i / (segmentDebug + numOfObjectsDebug)), new Vector3(1, 1, 3));
         }


Why does the above code work as expected (gets separation correctly), but the one below does not work? (I will put that in a separate comment)

avatar image AmasterAmaster · Jul 03, 2015 at 07:00 AM 0
Share
 if(segment == 1)
 {
     segment++;
 }
 
 //Get the distance between two limits (left and right)
 distance = Vector3.Distance(leftSide.transform.position, rightSide.transform.position);
 offset = distance / segment;
 //segment = leftSide.transform.position.x + rightSide.transform.position.x / numOfObjects;
 //float dist = $$anonymous$$athf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance / numOfObjects);
 //float dist = $$anonymous$$athf.InverseLerp(rightSide.transform.position.x, leftSide.transform.position.x, distance);
 
 //Set the position
 //someObject.transform.position = new Vector3(dist * numOfObjects, rightSide.transform.position.y, rightSide.transform.position.z);
 //someObject.transform.position = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, numOfObjects / (segment + 1));
 //someObject.transform.position = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, numOfObjects / (segment + numOfObjects));
 //Gizmos.DrawCube(Vector3.Lerp(new Vector3(leftSide.transform.position.x + offsetDebug, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, i / (segmentDebug + numOfObjectsDebug)), new Vector3(1, 1, 3));
 //someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, amount / (segment + numOfObjects));
 //someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, amount / (segment + 1));
 for(int i = 1; i <= amount; i++)
 {
         //someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, numOfObjects / segment);
         someObject.transform.position = Vector3.Lerp(new Vector3(leftSide.transform.position.x + offset, leftSide.transform.position.y, leftSide.transform.position.z), rightSide.transform.position, amount / (segment + numOfObjects));
 }
avatar image kingcoyote · Jul 03, 2015 at 03:58 PM 0
Share

In your second block, line 20, you are setting up a for loop, iterating i. But you never use i inside your for loop. Is that intentional?

avatar image kingcoyote · Jul 03, 2015 at 04:02 PM 0
Share

Another thing that you may not be aware of, with Lerp, the third parameter is a value between 0 and 1. I see some times where you are calling Lerp in a way that the third value is likely not between 0 and 1.

With Lerp, you give it two points (your leftSide and rightSide) and a value, between 0 and 1, and Lerp will return a point between your two points.

If you want 2 items evenly spaced between leftSide and rightSide, you would do this:

 var firstPos = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, 0.33f);
 var secondPos = Vector3.Lerp(leftSide.transform.position, rightSide.transform.position, 0.66f);

The 0.33 and 0.66 are 1/3 and 2/3. You have 2 objects, and 3 segments. If you have 8 objects and 9 segments, you would use 1/9, 2/9 ... 8/9.

avatar image AmasterAmaster · Jul 04, 2015 at 09:46 AM 0
Share

Thanks for helping me even if I may not be good at the math side of things, I will tinker with the code (and the advice) a little bit more and will get back to you with another comment if things don't turn so well (or if they do turn out well). As for the for loop being intentional, I think it was, but I need to utilize the index number "i", but I am working on it.

Show more comments

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

22 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

Related Questions

Pooling GameObjects with TextMesh ? 0 Answers

arc object's position using sin 1 Answer

How to deactivate objects that are children all at once? 0 Answers

How do i have multiple objects to rely on a objects position? 1 Answer

How Can I Set Object Position to Another Object? 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