Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Thomas-Hawk · Jun 07, 2017 at 11:35 PM · lerpslerpinterpolationsphericalvector math

Take my useful script - but help me improve it?

This script, automatically moves and rotates and objects through a serious of "steps" each with their own options like rotation, worldspace, smoothing, etc.

I am using the option for lerp or slerp to get a straight line between certain points, but a curved one for others.

It is working very well, here's an example of a platform moving akin to a familiar SM64, with debug lines and trail renderer to show the path:alt text

Here is the script in full: https://pastebin.com/EwpbuBTW

The problem, or thing I need help understanding:

The movement is "smoothed" between the steps, but with multiple steps, this results in a non-fluent motion, in the sense of, you can "tell" where the steps are, because it slows down to each one. I threw in some other variables and booleans to play with the values I was using, but to no avail - What's the magic to slerping but with a fixed rate,i.e, not slowing down to each step?

Am I overthinking this?

examp.png (46.0 kB)
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

2 Replies

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

Answer by jdean300 · Jun 08, 2017 at 06:05 AM

You are using lerp wrong (at least for what you want to accomplish). Vector3.Lerp(a, b, .5) returns the point half way between a and b. But if you repeatedly do a = Vector3.Lerp(a, b, .5) you are repeatedly moving half way to b but never quite getting there.

Eventually you get close enough and this line in your code if (Vector3.Distance (movingObj.transform.localPosition, steps [i].pos) <= .1f) causes you to move to the next point.

What you should be doing is lerping from the previous point (not the objects current position) to the next point according to to t. So something like this:

 movingObj.transform.localPosition = Vector3.Lerp (steps [i - 1].pos, steps [i].pos, t);

You're going to have to watch out for when i == 0 because you will get an index out of bounds exception, but hopefully you get the idea.

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 Thomas-Hawk · Jun 08, 2017 at 06:42 AM 0
Share

Ah, ok, that clarifies it a little bit. Thank you sir.

avatar image
0

Answer by _AkSeL_ · Jun 10, 2017 at 10:28 AM

You are actually slerping between the current object position (moving) and the next step position (fixed).

As your object move away from last step's position, the distance to next step's position decrease, so the slerping is actually "slowing down" the movement at each step. If you want to have a constant speed, you need to slerp between fixed positions, on a time-based factor, e.g. between "current" step and next step based on step duration.

Here is an example that works with your script:

 // The step we are currently "in"
 public int currentStep = 0;
 // The step we move to
 public int nextStep = 1;
 // Interpolation between currentStep and nextStep : 0 -> currentStep, 1 -> nextStep.
 public float interpolationFactor = 0f;
 // The time at the start of currentStep
 public float startTime = 0f;
 // Total duration of a step.
 public float stepDuration = 1f;

In the Start() function, we need to initialize startTime:

 void Start()
 {
     speed = steps[0].speed;
     smooth = steps[0].smooth;
     worldSpace = steps[0].worldSpace;
     startTime = Time.time;
 }

And then in the Update() function we do the slerping:

     void Update()
     {
 
         // Switch between steps based on time
         // Equivalent to your Next() function
         if (Time.time - startTime > stepDuration)
         {
             currentStep = nextStep;
             nextStep++;
             if (nextStep >= steps.Length)
                 nextStep = 0;
             startTime = Time.time;
         }
         // Interpolate based on time
         interpolationFactor = ((Time.time - startTime) / stepDuration);
         // Slerp
         movingObj.transform.position = Vector3.Slerp(steps[currentStep].pos, steps[nextStep].pos, interpolationFactor);
         movingObj.transform.rotation = Quaternion.Slerp(Quaternion.Euler(steps[currentStep].rot), Quaternion.Euler(steps[nextStep].rot), interpolationFactor);
 // [...]
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

68 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

Related Questions

Problem with slerp 0 Answers

How would I Lerp or interpolate this? 1 Answer

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers

Problem in moving via waypoint 0 Answers

Problem with Lerp/Slerp 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