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
1
Question by Kyuu · Jan 09, 2015 at 04:36 PM · vector3listlinerendererpathpercentage

Need to find a point in a line using a percentage and a Vector3 list.

Hello!

I need some advice or direction on how to take on a problem I'm facing.

I have a Vector3 list. These Vectors resemble a line (Visualised by a Line Renderer). Image example: alt text

This line is created using the Vector3 list which is created by "drawing" with the mouse. I have the length of the line. Since the length varies, it won't be a fixed value. What I want to accomplish, is to use a percentage (of the length of the line), to find a Vector3 point in this line.

Example:

In the picture above, say I would like to get the point at 20% This is the point where I'd like to get the Vector3 of: alt text

(Not sure if this the exact point, but I hope you get the idea)

I have no idea how to do this, so could you please help me?

I have googled around using percentages ect. but those were for Vector's between 2 points; this is a whole list of Vectors. I couldn't find anything about that.

Friendly regards.

screen shot 2015-01-09 at 14.32.41.png (7.9 kB)
screen shot 2015-01-09 at 142.png (881 B)
Comment
Add comment · Show 4
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 Victory Dan Greene · Jan 09, 2015 at 04:54 PM 0
Share

If you know the length of the whole list, L, and the fraction of that length, P, then the target length, from the start to your desired output point, is their product, L * P.

(If P > 1 or P < 0, your output point is an endpoint.)

For each point in your list, add the distance from there to the next point, until you exceed the target length.

Then your output point is the last point you ended at, offset by a vector with direction from there to the previous point and magnitude equal to the difference between your accumulated length and the target length.

avatar image Victory Dan Greene · Jan 09, 2015 at 05:06 PM 0
Share

pseudocode:

 if ( p < 0 ) return list[0] ;

 else if ( p > 1 ) return list[ list.length-1 ] ;

 else {

     targetLen = totalLen * p ;
     traveledLen = 0 ;

     for ( i=0 ; traveledLen<targetLen ; i+=1 ) {
         start = list[i] ;
         end = list[i+1] ;
         traveledLen += distance( start, end ) ;
     }

     backtrack = new Vector3( direction:start-end, magnitude:traveledLen-targetLen ) ;
     return end + backtrack ;

 }


avatar image Kyuu · Jan 12, 2015 at 12:34 PM 0
Share

Just a quick note, this seems pretty solid. I am not able to implement it until tomorrow, so I will come back to this! Thank you for your answer though, it definetly helped me already!

avatar image Kyuu · Jan 14, 2015 at 09:40 AM 0
Share

This together with the other answer gave me the direction I needed, so I want to thank you as well! If you posted your answer as an answer, and not a comment, I would also select it as an accepted answer. Thank you again!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Jan 09, 2015 at 04:55 PM

This is untested but should work, note that the length of the curve supplied is obviously not just the last point minus the first point!

 Vector3 FindPoint(Vector3[] vectors /* your vector array */, float length /* length of curve */, float p /* percentage along the line 0 to 1 */){
     if(vectors.Length < 1){
         return Vector3.zero; // if the list is empty
     }else if(vectors.Length < 2){
         return vectors[0]; //if there is only one point in the list
     }
     
     float dist = length*Mathf.Clamp(p, 0, 1);
     Vector3 pos = vectors[0];
     
     for(int i = 0; i < vectors.Length-1; i++){
         Vector3 v0 = vectors[i];
         Vector3 v1 = vectors[i+1];
         float this_dist = (v1-v0).magnitude;
         
         if(this_dist < dist){
             dist -= this_dist; //if the remaining distance is more than the distance between these vectors then minus the current distance from the remaining and go to the next vector
             continue;
         }
         
         return Vector3.Lerp(v0, v1, dist/this_dist); //if the distance between these vectors is more or equal to the remaining distance then find how far along the gap it is and return
     }
     return vectors[vectors.Length-1];
 }

Hope that helps, let me know if it produces any errors :)

Scribe

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 Kyuu · Jan 12, 2015 at 12:35 PM 0
Share

Thanks you for your answer! Looks like this should work as well, so thank you! I'm unable to test this until tomorrow, but I'll get back to this then!

avatar image Kyuu · Jan 14, 2015 at 09:38 AM 0
Share

While I couldn't implement it out of the blue, this gave me the right direction (as well as the other comment). So thank you very much for your time and effort! You've helped me a lot =]

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to Sync List 1 Answer

How do you generate a line between objects that are only instantiated upon game Start ? 1 Answer

slowly rotate a object *need quick fix* 0 Answers

How to combine Vector3 arrays? 3 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