- Home /
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;
}
}
}
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;
}
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.
Ok but then is it solved now or are you still after some help?
Your answer
Follow this Question
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