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 /
  • Help Room /
avatar image
0
Question by no00ob · Oct 22, 2018 at 07:13 PM · animationlerpfloatsmoothmathf

How can you increase a float to certain number and stop, while it is being used.

So I have a blend tree that has a variable speed. But I need to increase it smoothly from walk speed to run speed while I'm at the same time using it to translate the object forward, this is required so the animations won't look so instant and choppy any help? I tried this method myself that the script shows, still doesn't work does it instantly also I tried placing the Mathf.Lerp straight into the line where the current speed is right now didn't work. I have also tried Mathf.SmoothStep didn't work either.

Script:

  void Movement(float v, float h)
 {
     if (h != 0f || v != 0f)
     {
         Rotate(v, h);
 
         if (!Input.GetButton("Run") || Input.GetButtonUp("Run"))
         {
             if (ran)
                 currentSpeed = Mathf.Lerp(runSpeed, walkSpeed, 3f);
             if (currentSpeed == walkSpeed)
                 ran = false;
 
             playerAnimator.SetFloat("speed", 1);
             transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
         } else if (Input.GetButton("Run"))
         {
             playerAnimator.SetFloat("speed", 3);
             currentSpeed = Mathf.Lerp(walkSpeed, runSpeed, 3f);
             transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
         }
         if (Input.GetButton("Run"))
         {
             ran = true;
         }
         idle = false;
     }
     else
     {
         playerAnimator.SetFloat("speed", 0);
         idle = true;
     }
 }

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 CitizenVeen · Oct 23, 2018 at 02:36 PM

If I understand you correctly all you want to archieve is a steady rise of a var, called currentspeed, between its minimum, called walkspeed, to its maximum, called runspeed?

You are using Mathf.Lerp wrong. That equation gives you a value between its minimum a and its maximum b, based on what your third value c is. Example:

  Mathf.Lerp(0,200,0);
  // gives you 0
  Mathf.Lerp(0,200,0.5f); 
 // will give you 100
 Mathf.Lerp(0,200,1); 
 // will give you 200

As you assign 3f to your third value, mathf.lerp will allways give you the same value. What you need to do is steadely increase the value of c, the following code should increase the currentSpeed between your minimum, walkSpeed to your maximum, runSpeed in 2 seconds from the moment the scene starts, then stay at the runSpeed.

 float increaser;
 float secondsTillMaxSpeed = 2;
 
 void Update() 
 {
 increaser = Mathf.Clamp01(Time.time / secondsTillMaxSpeed);
 
 currentSpeed = mathf.Lerp(walkSpeed, runSpeed, increaser);
 
 }

I think it really helps to understand what mathemathical equasions actually do when using them, so when they don't perform how you expect them, you don't get baffled as to what to do about it. This video: https://www.youtube.com/watch?v=beWwwxn7dLQ&list=PLFt_AvWsXl0fnA91TcmkRyhhixX9CO3Lw∈dex=16 gives you an excellent explanation as to what Mathf.Lerp does in very much the same situation as you are trying to use it.

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 no00ob · Oct 23, 2018 at 07:29 PM 0
Share

Thank you very much! And thank you for the video, It seems like I didn't quite understand lerp correctly now I know what it does exactly :)

avatar image
0

Answer by no00ob · Oct 24, 2018 at 07:34 PM

(Edit: Umm... how would I decrease it? I can't get that to work now...) I realized that the animations speed value was different to my walk speed so I was doing the "Blending" between walk and run for nothing, but that didn't work either to begin with so thanks still as this helped a lot.

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

306 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Lerp between two int vaules smoothly 4 Answers

Smooth movement for a 3rd person camera 2 Answers

Smooth camera to target Y-height 3 Answers

Play animation controlled 0 Answers

Interpolated movement in a circle 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