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 Ravel · Jan 16, 2017 at 07:38 AM · c#vector3distancepathmeasure

How to get a "Traveled" vector point along a path with some width?

For example I have a path, that consists of 10 cross sections. Each cross section defines a center point and edge points. So in total I have 30 vector points that define my path, which the traveler X can travel. My goal is to receive the traveled distance along the center points, centerpoints[currentPoint].

My current approach relies on dot product, but as soon as the traveler goes away from the center I get incorrect results. I am currently not using the edge points, but they are there if needed, because they define the width limits to the path.

Here is the code of my approach:

         int points = 10;    // size of the path list
         int pointNumber = 0; // find the closest point in path list to traveler
         
         // Measure the total length of the path and store distance per segment
         float accumulateDistance = 0;        
         for (int i = 0; i < path.Count; i++)
         {
             Vector3 p1 = path[(i) % points].centerPoint;
             Vector3 p2 = path[(i + 1) % points].centerPoint;
 
             if (p1 != null && p2 != null)
             {
                 accumulateDistance += (p1 - p2).magnitude;
                 path[i].dist = accumulateDistance;
             }
         }
         
         // Measure the traveled distance along path
         Vector3 currentPoint = path[pointNumber].centerPoint;
         Vector3 nextPoint;
         if (r.currentWaypoint < path.Count - 1)
             nextPoint = path[pointNumber + 1].currentPoint;
         else
             nextPoint = path[0].currentPoint;
             
         Vector3 forward = (nextPoint - currentPoint);
         Vector3 relativePos = traveler.transform.position - currentPoint;
 
         float distAdd = Vector3.Dot(relativePos, forward.normalized);
         progressDistance = path[pointNumber].dist + distAdd;

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

Answer by ifisch · Sep 02, 2020 at 11:33 PM

If you want the question answered, you should include a diagram.

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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Shortest distance between two meshes/colliders 3 Answers

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Are Unity distances that small? 1 Answer

Decrease value at half the speed it's increased at? (using Vector3.Distance) 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