• 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 Amposter · Sep 02, 2016 at 01:45 PM · prediction

Approximating time and position of an object along a BezierCurve in Unity 5

I currently have an object that I'm moving a long a Bezier curve by sampling a few points along the curve and then just using Unity's MoveTowards to move the object to each point at a constant speed.

Before I do this though, I want to approximate the time that the object will reach the each of the points which I am currently doing by calculating the distance between the points and then just dividing that by the speed of the car.

The values I get are off usually off by one or two decimal places and what I've noticed is that the time values change. Does Unity do some things under the hood that I should take into account and is it possible to get more accurate results, at least to 1 decimal point?

EDIT: Here's some of the code so you can see what I'm trying to do:

This is the internal simulation

     float initialTime = Time.time;
     Debug.Log("Initial time simulation: " + initialTime);
     float distance = 0;
     Vector3 curr = transform.position;
     Vector3 next;
 
     for (int s = 1; s < steps+1; ++s)
     {
         float segment = (1.0f - (s/ (float)steps)); 
         next = curve.GetPointAt(segment);
         next.y = curr.y; //Only using x and z of bezier curves
         distance += (next - curr).magnitude;
         curr = next;

         float elapsedTime = distance / speed;
         output[s - 1] = new KeyValuePair<float, Vector3>(initialTime+elapsedTime, point);
     }

This is what actually gets simulated

     int counter = 1;
     Debug.Log("Initial Time Actual: " + Time.time);
     Vector3 toPoint = curve.GetPointAt((float)(steps - counter) / (steps));
     toPoint.y = transform.position.y;
     transform.LookAt(toPoint);
     while (counter <= steps)
     {
         
         Vector3 newPos = Vector3.MoveTowards(transform.position, toPoint, speed * Time.deltaTime);
         transform.LookAt(newPos);
         transform.position = newPos; 

         if (transform.position == toPoint)
         {
             Debug.Log("CurrTime: " + Time.time + " " + transform.position);
             ++counter;
             toPoint = curve.GetPointAt((float)(steps - counter) / (steps));
             toPoint.y = transform.position.y;
             transform.LookAt(toPoint);
         }
         yield return null;
     }
Comment
Add comment · Show 5
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 troien · Sep 02, 2016 at 06:08 PM 0
Share

I think it would help if you post your current piece of code that contains the $$anonymous$$oveTowards part (specifically what you provide as the maxDistanceDelta argument)

And the current code you use to calculate that approximate time.

I'm expecting its either a mistake in your code or perhaps because $$anonymous$$oveTowards slows down a tiny bit at the end (in the end the distance delta between the 2 points is less then the maxDistancedelta, therefore you'll move a bit slower as intended), this is probably not noticeable by looking at it with your eyes, especially when you have a lot of small distances, but this also adds up to perhaps a measurable value when you have a lot of small distances.

avatar image Bonfire-Boy · Sep 02, 2016 at 06:47 PM 0
Share

Do you just mean that your approximation is different to how long it actually takes? Is that difference any more than you'd expect simply from the fact that time is quantised in the engine?

avatar image Amposter · Sep 05, 2016 at 09:58 AM 0
Share

I've added the code.

avatar image Bonfire-Boy · Sep 05, 2016 at 12:57 PM 1
Share

Have you thought about the question I asked above? I'm not convinced there's a problem here. Let's say your prediction says exactly 10s. Because of the quantisation of time, it is very unlikely that a frame will be rendered on exactly 10s, there will be a frame just before 10s and one just after. So your prediction can only ever be an approximation; you have to expect there to be a difference. And, for example, if deltaTime was averaging to about 0.02s, then that difference would often affect the second decimal place and sometimes the first, which would fit with your description.

So for me, any discussion of whether the prediction is turning out to be accurate has to include some consideration of deltaTime. Simply logging it along with Time.time on line 15 would help. Or looking to see what the value of Time.time is on the frame before it hits. The proper test of the accuracy of your predicted value is whether or not the time taken is >= your prediction on the frame when the movement finishes, and < your prediction on the preceding frame. Or put another way, is there a frame when your prediction says the movement should have finished, but it hasn't?

avatar image Glurth · Sep 05, 2016 at 07:30 PM 0
Share

In the movement code you aim at the point and move towards it. But you make no consideration of what happens when you reach it before the end of the cycle, when it still has some "movement" remaining. Vector3.$$anonymous$$oveTowards can be a bit deceptive this odd way that is uses that last parameter, but that's why it's called **max**DeltaDistance. This is why I'd prefer to just do the vector arithmetic, and see what movement distance remains.

(untested, example only)

 while (counter <= steps)
         {
             Vector3 travelVector3 = toPoint - transform.position;
             Vector3 travelDirection = travelVector3.normalized; //same direction as travelVector, but has magnitude of one.
             float distanceToTravel = travelVector3.magnitude;
             if (distanceToTravel >= moevement_remaining)
             {// we wont reach it this cycle
                 transform.position += (travelDirection * moevement_remaining);
                 moevement_remaining = 0;// use all movement to get us towards it
             }
             else
             { // we WILL reach, and probabaly, pass it this cycle
                 transform.position += (travelDirection * distanceToTravel);
                 // or faster and equivilent (but less explicit for an example)
                 transform.position = toPoint;
 
                 moevement_remaining -= distanceToTravel; //reduce movement ramaining for this cylce
             }
             if (moevement_remaining <= 0.0f)//all movement for cycle is now spent
             {
                 counter++;
                 moevement_remaining = speed;
                 toPoint = curve.GetPointAt((float)(steps - counter) / (steps));
                 toPoint.y = transform.position.y;
                 transform.LookAt(toPoint);
             }
         }

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Trajectory prediction basketball 1 Answer

Billiard/Pool aiming issue 0 Answers

Network client side Prediction and gravity server-client different 1 Answer

Predict rotation angle for torque applied? 1 Answer

LineRenderer use to predict a path? 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