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 $$anonymous$$ · May 19, 2014 at 08:54 AM · animationlerp

Lerp animation duration

Hey guys, I'm having a problem animating a simple GameObject using the Vector3.Lerp function. Basically, I have a drawer I want to animate, so I have created a script which expose a public animationDuration variable that will allows me to adjust the open animation time. However, the problem I'm facing is that whatever value I set for this param, the drawer takes always more or less five seconds to complete. I know I have another way to do it, exposing a public speed variable that I can use as product in the Lerp t param, but in this way I would not have the full control of the animation duration in seconds. Can you guys give me a hand? Here's a simplified version of the script.

 using UnityEngine;
 using System.Collections;
 
 public class DrawerController : MonoBehaviour {
     
     public float animationDuration = 2.0f;
 
      float timer = 0.0f;
     float animationTimer = 0.0f;
     
     Vector3 closedPosition;
     Vector3 openPosition;
     
     void Start () {
         closedPosition = transform.localPosition;
         openPosition = closedPosition;
         openPosition.x += 0.20f;
     }
     
     void Update () {
         if(Input.GetKeyDown(KeyCode.G)){
              StartCoroutine("Open");    
         }
     }
     
     IEnumerator Open(){
 
         while(timer < openingTime){
             timer += Time.deltaTime;
             animationTimer = timer / animationDuration;
             transform.localPosition = Vector3.Lerp(closePosition, openPosition, animationTimer);
             yield return null;
         }
     }
     
 }
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 fafase · May 19, 2014 at 08:58 AM

You can try this below, it works for me:

 float ratio = 0;
 float duration = 0.5f; // this is the one that will control how long it takes
 // value is in second 
 float multiplier = 1 / duration;
 
 while (transform.localPosition!= openPosition){
     ratio += Time.deltaTime * multiplier;
     transform.localPosition = Vector3.Lerp(closePosition, openPosition, ratio);
     yield return null;
 }
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 $$anonymous$$ · May 19, 2014 at 12:13 PM 0
Share

Thanks for your help fafase. I think your solution works better then $$anonymous$$e and it's more elegant since it doesn't make use of any timer and check the GameObject position directly, plus, it helped me to figure out the real problem, so I will mark it as the best answer. Thanks again. Unfortunately, didn't solved the problem.

Basically I've found that everytime I wanted to change the animation duration, I changed the related animationDuration variable directly from the script because I needed to change the default value. The problem of doing this was that since the script was already attached to the drawers, the value was set by default to 20s (the value of this variable when I've attached the script to the objects the first time). So, the value from the script didn't reflected the editor value.

Changing the value from the editor or detach and attach the script again to the object solved the problem.

avatar image fafase · May 19, 2014 at 06:08 PM 0
Share

Ok but then is it solved now or are you still after some help?

avatar image $$anonymous$$ · May 19, 2014 at 10:17 PM 0
Share

It's solved now, thanks again for your help!

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to use Mathf.Lerp and Mathf.InverseLerp??? 1 Answer

Animation in Unity 4.3 0 Answers

Animation spins wildly after completed 0 Answers

How can I reassign animation curves to play on a child object (instead of through parent)? 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