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
0
Question by SassMaster · Sep 04, 2014 at 09:10 PM · c#movementlerpacceleration

Doing something wrong with lerp?

I'm relatively experienced with C#. I've been using it for software development and stuff like that, but I've never used it in Unity and that's probably why I don't what I'm doing wrong. Basically, I'm trying to lerp a the speed of an object to make it accelerate, and I'm using Lerp. I followed the tutorials, and I've experimented with it a lot, and looked for other answers, but something here is wrong, and I have no idea what it is.

 void testFunction()
     {
         float minSpeed = 0.0f;
         float maxSpeed = 10.0f;
         float currentSpeed = 0.0f;
         byte lerpSpeedMult = 10;
 
         if(Input.GetKey("w")) 
         {
             currentSpeed = Mathf.Lerp(minSpeed, maxSpeed, Time.deltaTime * lerpSpeedMult);
             transform.Translate (Vector3.forward * currentSpeed * Time.deltaTime); 
             print (currentSpeed);
         }
     }

I'm calling testFunction in Update, but I seriously have no idea what I'm doing wrong here. I'm using minSpeed as the "from" parameter of lerp, and maxSpeed as the "to" parameter, and making the current value of that lerp to be the currentSpeed, then I'm moving the ship by currentSpeed, but currentSpeed constantly remains zero, as well as all other variables. I know that I'm doing something that any kind of Unity veteran would spot, but yet again, I've never used C# in Unity before.

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

Answer by AyAMrau · Sep 04, 2014 at 09:18 PM

the third argument of lerp represents the progress between the two values. What you are doing here is supply the same value on every frame (very small value as well) so there will be no progress.

What you want to do is to accumulate the rate (Time.deltaTime * lerpSpeedMult) into a variable on each frame and then supply this variable to the lerp.

Here is how to do this in a coroutine - the reason to do that is because then you can keep the value of local variables, which is important for keeping the progress. Otherwise the progress would have to be a class variable

     void Start()
     {
         StartCoroutine(lerp());
     }
 
     private IEnumerator lerp()
     {
         float minSpeed = 0.0f;
         float maxSpeed = 100.0f;
         float currentSpeed = 1.0f;
         float lerpSpeedMult = 1.0f;
 
         float progress = 0f;
 
 
         //You can change this loop so the coroutine stops when you reached the max speed
         while(true)
         {
             currentSpeed = Mathf.Lerp(minSpeed, maxSpeed, progress);
             progress += lerpSpeedMult * Time.deltaTime;
             yield return null;
         }
     }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Distribute terrain in zones 3 Answers

What is wrong with this script ? if duration is 10 why the transform is moving in 11 seconds and not 10 ? 1 Answer

MouseWheel Lerp Smoothing Problem 1 Answer

JRPG style Movement using Vector3.Lerp jittering in place? 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