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 gouzarte · Jul 05, 2021 at 09:17 PM · lerpslidermathf.lerp

Mathf.lerp not finishing the lerp(age)

I am currently trying to make a Slider that smoothly goes from 0f to 100f, taking 100 seconds.

On my current way of making it work, that looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class swordui : MonoBehaviour
 {
     public Slider swordslider;
 
     public float valuee;
     public float speed = 0.01f;
     public bool commit;
 
     public void Awake()
     {
         commit = false;
         valuee = 100f;
     }
     public void settozero()
     {
         valuee = 0f;
     }
     public void commitmoment()
     {
         commit = true;
     }
     public void Update()
     {
         swordslider.value = valuee;
         if(commit == true)
         {
             valuee = Mathf.Lerp(swordslider.value, 100f, speed);
             if (swordslider.value == 100f)
             {
                 commit = false;
             }
         }
     }
 }

but this causes it to slow down by the end of the countdown. When I try to change it to:

             valuee = Mathf.Lerp(0f, 100f, speed * Time.deltaTime);

it only counts up to 1, and then stops lerping.

How can I make it fully count from 0f to 100f, taking 100 seconds to do that, and without stalling by the end?

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
1
Best Answer

Answer by tyruji · Jul 05, 2021 at 11:07 PM

In the first case, you experienced one of the Zeno's paradoxes. Basically you are overwriting the initial value of the interpolation over and over, which in turn makes the interpolation non-linear. Your second approach is better, but you are not accelerating the time, your value will stay the same forever. What you should do is:

 private float _timeAccel = 0;
 private float _start = 0.0f, _end = 100.0f;
 private float _speed = 0.01f;
 
 private void Update()
 {
         value = Mathf.Lerp( _start, _end, _timeAccel );
         _timeAccel += Time.deltaTime * _speed;
 }

Also, don't worry if _timeAccel exceeds 1, the Lerp will always clamp it between 0 and 1. Good luck!

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 gouzarte · Jul 06, 2021 at 10:18 AM 0
Share

This worked, thank you so much!

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

122 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

Related Questions

What's faster: Lerp or LerpUnclamped? (Performance) 1 Answer

how to use time on lerp 2 Answers

Mathf.Lerp happens instantly 2 Answers

Problem with Mathf.Lerp 1 Answer

How to lerp smoothly between two numbers? 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