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 midomido · Feb 14, 2013 at 11:07 PM · pathlinesplinealong

Cloning An Object Along A Path Between 2 Points

What i need to make is draw a line not a straight line but a line with spaces, To do this I have 2 points and a prefab(cube) which i want to instantiate many times between those 2 points and the prefab should be instantiated in amounts according to the distance between the 2 points. Thank you.

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

Answer by hoy_smallfry · Feb 15, 2013 at 01:02 AM

(I'm assuming you know how to instantiate a prefab)

This is a problem easy solved by linear interpolation, or "lerp" as its often times called.

The idea is that you treat the 3D positions as 3D vectors, and therefore you can find the vector between them by subtracting one vector from another:

alt text

This vector between the two points has a length, and according to vector multiplication, the vector can be scaled up by a number, which multiplies its length:

alt text

If you multiplied it by 3, the resulting vector from the multiplication is 3 times it's size. But, if we multiplied the vector by 1 the result is same vector, and as you can see by the first picture, that would be pointed exactly at the spot A is pointed at. If we multipled it by 0, then the vector would have no length, which is essentially at point B.

SO... depending on what value you multiply by between 0 and 1, you can get a value that runs along the black vector line in the first image. If the value is 0.25, then it is a quarter of the way to A from B, etc.:

alt text

Thankfully, Unity provides a function for linear interpolation:

 Vector3 point = Vector3.Lerp(startPoint, endPoint, percentage);

So, you can essentially use a for loop from from 0 to 1 that increases by (1.0f/N), where N is the number of dots you want to display in between the two points, and pass the loop value in as the percentage.


7874-vector_subtraction.png (61.2 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 midomido · Feb 15, 2013 at 08:56 AM 0
Share

Thank you so much, your answer was extremely detailed and helpful.

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

11 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

Related Questions

How can I collect the positions of my waypoint objects which mark out a path or route? 2 Answers

iTween Path Rotation 0 Answers

How to make a line between object in Editor Window 1 Answer

how to move along path with user controll 0 Answers

optimal race line 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