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 Lostrick · Jan 25, 2015 at 06:57 AM · vector3.lerp

Vector3.Lerp slow first and become faster

is there and alternative to vector3.Lerp that makes it slow at first and get faster, rather than the other way?

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 karma0413 · Feb 04 at 01:21 PM 0
Share

I realize it is 7 years later and you probably found a solution by now. But, for anyone else googling this problem...I have attached the solution below.

3 Replies

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

Answer by tanoshimi · Jan 25, 2015 at 07:06 AM

Lerp is not fast at first, slow later. It is linear (that's what the 'L' stands for) with the delta supplied as the third parameter.

However, it is commonly misused. You might want to read the following to explain how:

  • http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

  • http://theantranch.com/blog/using-lerp-properly/

To have a smoothing function that eases in, you need to provide an appropriate function as the third parameter (some sort of exponential function, perhaps)

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 Lostrick · Jan 25, 2015 at 07:22 AM 0
Share

thanks, i'm always having trouble using lerp, i didn't realize it's like this

avatar image jessy-girard · Nov 02, 2016 at 01:44 PM 0
Share

Please note that in the first article you linked, the author writes percentage > 1.0f. This is called a ratio.

Also, he overcomplicates the lerping mechanic.

avatar image karma0413 · Feb 04 at 01:16 PM 0
Share

You didn't answer the guys question. You just told him how lerp works. I have attached code below which actually answers his question.

avatar image
1

Answer by karma0413 · Feb 04 at 01:14 PM

I REALIZE this is a Late-Post (years later), but NOBODY answered the man's question!

I will apologize for the lengthy reply, but it is necessary to understand the math for "why" it works... Or you could just skip to the bottom and final code snipet

In case anyone else is Googling this exact same question, the solution is as follows:

First, we need to develop a self-iterative way for increasing value. In other words, we need to find a number which increases on its own. In this case Time.time

Let's call this idea of starting slow and gradually moving fast as ExponentialLerp

As soon as we start the the ExponentialLerp, we need to set a timeStamp

 float timeWeStarted = Time.time;


So anytime, you want to ExponentialLerp, you must find out how much time has elapsed since we started ExponentialLerping...

 float elapsedTime = Time.time - timeWeStarted;
 float stepAmount = elapsedTime;

 // FOR VECTOR3s
 Vector3.Lerp (origin, destination, Mathf.MoveTowards (0f, 1f, stepAmount));
 // FOR FLOATS
 float variableToLerp = Mathf.MoveTowards (0f, 1f, stepAmount);
 // FOR COLORS
 Color colorToUse = Color.Lerp (originalColor, destinationColor, Mathf.Towards (0f, 1f, stepAmount));


As you likely have guessed by now, so far every time this code is run it is additive. In other words, right now we are only moving in a positive step direction without actually going faster.

Let's do that now!

I have a graphing calculator, but you could just rely on the famous Google to show you what it looks like. So go to google and search this "y = x^2"
Let's pretend for a moment that X is the elapsedTime, and Y is the stepAmount. This means for each moment in time that passes, the step amount will get exponentially faster!
alt text



The code to achieve this would look like this...

 float elapsedTime = Time.time - timeWeStarted;
 float stepAmount = Mathf.Pow (elapsedTime, 2f);
 // FOR VECTOR3s
 Vector3.Lerp (origin, destination, Mathf.MoveTowards (0f, 1f, stepAmount));
 // FOR FLOATS
 float variableToLerp = Mathf.MoveTowards (0f, 1f, stepAmount);
 // FOR COLORS
 Color colorToUse = Color.Lerp (originalColor, destinationColor, Mathf.Towards (0f, 1f, stepAmount));

As you can see, we simply define our stepAmount as taking the elapsedTime to a power of 2.

In my case, the speed at the tail end was not fast enough to end the ExponentialLerp. As such all we need to do is increase the power of 2 until it's fast enough. Maybe a power of 10???

Again, go to google and look a the power curve by searching "y = x^10"
alt text


As you can see, now we are really going fast at the trail-end of this ExponentialLerp.

In my particular case though, I found that the time it took from from starting until it started ramping up at the end was too long!!! That flat-line in the last picture was too long....

All we have to do is take our stepAmount value (before raising it the power) and multiply it by some variable.

FINAL CODE and the ANSWER to the Man's QUESTION

 // This at the top of your script
 public float speedOfChange = 2f;
 public float exponentialModifier = 10f;
 public float timeWeStarted;
 
 
 // This code should be maybe in a function that you call which starts the ExponentialLerp
 timeWeStarted = Time.time;
 
 
 // This is somehwere in the update where you are trying to ExponentialLerp
 float elapsedTime = Time.time - timeWeStarted;
 float stepAmount = Mathf.Pow (elapsedTime * speedOfChange, exponentialModifier);
 // FOR VECTOR3s
 Vector3.Lerp (origin, destination, Mathf.MoveTowards (0f, 1f, stepAmount));
 // FOR FLOATS
 float variableToLerp = Mathf.MoveTowards (0f, 1f, stepAmount);
 // FOR COLORS
 Color colorToUse = Color.Lerp (originalColor, destinationColor, Mathf.Towards (0f, 1f, stepAmount));

x-to-10.jpg (34.9 kB)
x-to-2.jpg (20.9 kB)
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
avatar image
-1

Answer by jojo_game_studios · Jul 27, 2019 at 04:28 PM

@Lostrick I know that this is late, but you can spherically interpolate the the Vector using Vector3.Slerp. The Slerp method is more resource intensive, but it creates a smooth in and smooth out effect to both Quaternion rotations and Vector movements.

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 Bunny83 · Jul 27, 2019 at 06:06 PM 0
Share

Huh? No it does not. Slerp is also a linear interpolation but for vectors and ensures a correct spherical interpolation. It does not ease in any way. Where did you get that form? Have you actually read the documentation? Slerp only makes sense for rotations or direction vectors. Slerp will ensure that the vector keeps its length while interpolating.


You may also want to read the wikipedia page on slerp:

It refers to constant-speed motion along a unit-radius great circle arc, given the ends and an interpolation parameter between 0 and 1.


If you want easing you can simply use one of the classic hermite spline polynomials and feed your t value through that function before using it in Lerp / Slerp.

avatar image jojo_game_studios · Jul 27, 2019 at 06:22 PM 0
Share

@Bunny83 Spherically Interpolating does create an ease in/ease out effect because the Vector or Quaternion does not change equal amounts as compared to linear interpolating. Spherically interpolating changes slower at low and high percentages and faster at middle percentages.

avatar image Bunny83 jojo_game_studios · Jul 27, 2019 at 08:52 PM 1
Share

No, it does not. You don't seem to understand what spherical means, It moves at constant angular speed. The motion represents an arc ins$$anonymous$$d of a straight line, that's the only difference between Lerp and Slerp. See this video for a visualization. Slerp will always rotate along the shortest arc.


edit

Try attaching this script to a sphere in your scene. It will move the object from (0,5,0) to (5,0,0) and back. $$anonymous$$eep in $$anonymous$$d that Slerp is for direction vectors and not positions as the arc will always be around (0,0,0). This script will actually measure the speed at which the object moves along the arc and it is constant throughout the movement.

 using UnityEngine;
 
 public class SlerpTest : $$anonymous$$onoBehaviour
 {
     Vector3 old;
     float speed;
     float t;
 
     void Update ()
     {
         old = transform.position;
         t = $$anonymous$$athf.PingPong(Time.time * 0.5f, 1f);
         transform.position = 5f*Vector3.Slerp(Vector3.up, Vector3.right, t);
         Vector3 diff = transform.position - old;
         speed = diff.magnitude / Time.deltaTime;
     }
     
     private void OnGUI()
     {
         GUILayout.Label("Current speed:" + speed);
         GUILayout.Label("Current t:" + t);
     }
 }

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

6 People are following this question.

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

Related Questions

Creating a retractable grappling hook 1 Answer

Move player to mouse position never go to exact position 0 Answers

Lightning Using A Line Renderer 0 Answers

Vector3.Lerp doesn't work! 1 Answer

Unable to move Gameobject properly and smoothly to where mouse clicked , using Vector3.Lerp 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