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 /
  • Help Room /
avatar image
0
Question by Crawdad · May 31, 2020 at 10:06 PM · pathfollowingtravel

How to make object follow line vectors

I'm using the below code to create a line which shows an objects intended path of travel. It works really well, but I can't for the life of me figure out how to use the vectors created to have an abject follow that path at a set speed. Would really appreciate someone pointing me in the right direction.

 void DrawQuadraticBezierCurve(Vector3 point0, Vector3 point1, Vector3 point2)
     {
         lr.positionCount = 200;
         float t = 0f;
         B = new Vector3(0, 0, 0);
         for (int i = 0; i < lr.positionCount; i++)
         {
             B = (1 - t) * (1 - t) * point0 + 2 * (1 - t) * t * point1 + t * t * point2;
             lr.SetPosition(i, B);
             t += (1 / (float)lr.positionCount);
         }
     }
Comment
Add comment · Show 1
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 Crawdad · May 31, 2020 at 11:32 PM 0
Share

Here's a visual.

alt text

examplepic.png (16.9 kB)

1 Reply

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

Answer by tadadosi · Jun 01, 2020 at 01:22 AM

Really dirty attempt to do it. Stored the points on a Vector3[] array and used a simple logic on Update to switch between each vector, passing its position to the gameobject transform.position. There should be a better way of doing this, this is just a simple test that could give you a clue to come up with a better answer.


 public float stepDuration = 0.01f;
 public bool isActive;
 private Vector3 B;
 private Vector3[] BPoints;
 private int index;
 private float t;
 
 private void Update()
 {
     if (isActive)
     {
         t += Time.deltaTime;
         if (t / stepDuration >= 1)
         {
             index++;
             t = 0.0f;
         }
         if (index <= BPoints.Length - 1)
         {
             Vector3 targetPosition = new Vector3(BPoints[index].x, transform.position.y, BPoints[index].z);
             transform.position = targetPosition;
         }
     }
 }
 
 void DrawQuadraticBezierCurve(Vector3 point0, Vector3 point1, Vector3 point2)
 {
     BPoints = new Vector3[200];
     lr.positionCount = 200;
     float t = 0f;
     B = new Vector3(0, 0, 0);
     for (int i = 0; i < lr.positionCount; i++)
     {
         t += (1 / (float)lr.positionCount);
         B = (1 - t) * (1 - t) * point0 + 2 * (1 - t) * t * point1 + t * t * point2;
         lr.SetPosition(i, B);
         BPoints[i] = B;
     }
 }
Comment
Add comment · Show 3 · 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 Crawdad · Jun 01, 2020 at 02:34 AM 0
Share

That worked! Thanks much. I'd been banging my head against that for hours. Any ideas on how I could adjust the travel speed? I tried changing stepDuration but it seemed to have no effect.

avatar image tadadosi Crawdad · Jun 01, 2020 at 02:46 AM 0
Share

Possibilities that I can think of:

  1. A variable to change the amount of positions in your bezier curve, by increasing or reducing that amount, it should move slower or faster.

  2. A variable to change the index added value, like index += step; (a bigger value should make it go faster)

  3. $$anonymous$$ix both things to have more control.

avatar image Crawdad · Jun 01, 2020 at 07:08 PM 0
Share

$$anonymous$$ore great advice, Tadadosi. $$anonymous$$uch appreciated! increasing the index value did the trick. You've saved me a lot of time.

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

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

How can you make a path with a switch in it? 2 Answers

How to move an Object in a path? 0 Answers

Doing an Update Function only once 1 Answer

How to draw a path from players's position to the target position where i touch? 0 Answers

2D NPC Movement 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