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 FuryFight3r · Mar 14, 2020 at 02:47 AM · movementgameobjectlerp

Lerp Gameobjects..

I know this topic has been plastered all over, but it is unbaringly horrid to try and traverse through many different peoples methods to find what I am looking for.

All I am after is something the moves from A to B within a time frame thats it. no special features, no smoothing, just a basic, in and out within a time.

My current code, which obviously doesnt work (works okay. but doesnt have a 'smooth' movement, it just goes from a to b in an instant, how can I make it so the 'speed' actually is taken into accordance?

     public void openBottom()
     {
         bottomDraw.transform.localPosition = new Vector3(bTempX, bTempY, Mathf.Lerp(closePos, openPos, speed));
         bottomOpen = true;
     }
 
     public void closeBottom()
     {
         bottomDraw.transform.localPosition = new Vector3(bTempX, bTempY, Mathf.Lerp(openPos, closePos, speed));
         bottomOpen = false;
     }
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
Best Answer

Answer by Namey5 · Mar 14, 2020 at 09:36 AM

If you want something that moves linearly between A and B over a set period of time, then you need to keep track of time itself;

 //Lerp start
 public Vector3 start;
 //Lerp end
 public Vector3 end;
 //Total time of the lerp in seconds
 public float seconds = 2f;
 
 private float startTime = 0f;
 private bool runFunction = false;
 
 void Update ()
 {
     //Start the coroutine once when the space key is pressed
     if (Input.GetKeyDown (KeyCode.Space))
         StartCoroutine (LerpPosition (start, end, seconds));
 
     //Alternatively, run the function and set the current time when the mouse is clicked
     if (Input.GetKeyDown (KeyCode.Mouse0) && !runFunction)
     {
         startTime = Time.time;
         runFunction = true;
     }
 
     if (runFunction)
     {
         LerpPosition2 (start, end, seconds);
     }
 }
 
 private bool isRunning = false;
 
 //I'm using an enumerator to make things a bit easier, but there's also an example with a normal function below
 IEnumerator LerpPosition (Vector3 a, Vector3 b, float s)
 {
     if (isRunning)
         yield break;
 
     isRunning = true;
 
     float startTime = 0f;
     float time = startTime;
 
     while (time < s)
     {
         transform.position = Vector3.Lerp (a, b, (time - startTime) / s);
         time += Time.deltaTime;
         yield return null;
     }
 
     isRunning = false;
 }
 
 void LerpPosition2 (Vector3 a, Vector3 b, float s)
 {
     float time = (Time.time - startTime) / s;
 
     transform.position = Vector3.Lerp (a, b, time);
 
     if (time >= 1f)
         runFunction = false;
 }
Comment
Add comment · Show 2 · 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 FuryFight3r · Mar 15, 2020 at 04:50 AM 0
Share

Thanks so much for the answer, I had a quick read over this lastnight as I was just heading to bed, but put it into effect this morning and it works great.. Again thanks! Has this recently been modified the way this works? the only way I knew how to lerp an object was using a similar way I had shown in my code above, unless Im thinking of something different.

avatar image Namey5 FuryFight3r · Mar 15, 2020 at 05:26 AM 0
Share

The way you did it is definitely the most common answer, but it isn't really correct. The idea of 'pos = Lerp (pos, target, speed * Time.deltaTime)' is basically a smoothing function - with every new frame the speed at which the current position approaches the target is halved. It's convenient because it is self contained, but really doesn't work as proper linear interpolation (which my version above is). On top of that, the way you've written it above doesn't work either because it is constant - it will always return the point that is speed% between A and B, rather than moving between them over time (which involves interpolating between the current position and a target, rather than two targets).

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

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

Object moves to 0,0,0? (Just want to know why it works) 1 Answer

Lerping from one position to another in a specific amount of time, no matter the distance 0 Answers

Make Lerp or other more fluid or continuous 3 Answers

Game Object not moving in Canvas 0 Answers

Vector3.Lerp not working? 5 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