- Home /
Is Lerp acting weird?
Here is my code.
#pragma strict
#pragma implicit
#pragma downcast
public var theCamera : Transform;
function OnMouseUp()
{
theCamera.position = Vector3.Lerp(theCamera.position, transform.position, Time.time);
}
Instead of moving nicely over 1 second, it moves instantly. This script is attached to a Gameobject (with a collider), not a GUI Object.
Am I doing something wrong? Or is there a glitch?
Thanks in advance.
Dave
Answer by Eric5h5 · Dec 06, 2012 at 12:44 AM
OnMouseUp only runs once, when the mouse button is released. Lerp is not a function that "runs over time" or anything, it's just a very simple math function that returns a value immediately. (Specifically, (b - a) * t + a, where t is clamped between 0 and 1.) Also you should not use Time.time in the 3rd parameter, since it needs a value between 0 and 1.
If you want movement over time, it's generally easiest to use a coroutine, like this.
So what you're saying is that
I shouldn't use Lerp in On$$anonymous$$ouseUp
I shouldn't use theCamera.position, I should only reference static objects like pointTo$$anonymous$$oveTo.position
I shouldn't put Time.time in the 3rd parameter
I shouldn't use Lerp in this case anyways
Is this right?
Nope, the only thing I said is #3. Please click on the link I provided about using a coroutine.
I mean Bunny83 and you together seemed to say those four.
Perhaps I'm thick, but that 'coroutine script' is much too long...I only want one, maybe two lines of code, preferably that I can insert in my On$$anonymous$$ouseUp. Could (not may) I use a line or two of your script and make it work?
I really don't want anything complicated for this application.
I don't think you've actually read that page. You put the script in your project, and then call the functions as needed (with one line of code). Please at least look at the example usage code. Every line of code in the $$anonymous$$oveObject script is necessary for it to work as documented.
Answer by Bunny83 · Dec 06, 2012 at 12:43 AM
You do a lot worng ;) Lerp is ment to interpolate between two fix positions / values. You use the current position as start value.
So for example when your camera is at 0,0,0 at the beginning and your target position is at 100,0,0 it will process like this:
Frame start end t resulting value
0 0 100 0 0
1 0 100 0.05 5
2 5 100 0.1 14.5
3 14.5 100 0.15 27.325
4 27.325 100 0.2 41.86
5 41.86 100 0.25 56.395
You need to use the same start and end position
Frame start end t resulting value
0 0 100 0 0
1 0 100 0.05 5
2 0 100 0.1 10
3 0 100 0.15 15
4 0 100 0.2 20
5 0 100 0.25 25
Lerp is not black magic. It's very very simple. That's how it's implemented:
function Lerp(from : Vector3, to : Vector3, t : float)
{
return from + (to - from) * Mathf.Clamp01(t);
}
Your answer
Follow this Question
Related Questions
I'm using TRANSLATE but want to use LERP. C# 2 Answers
How do I translate around a circle? 3 Answers
Why my camera rotates around gameobject 1 Answer
How to move camera from point A to B 1 Answer
Camera moving in one direction? 2 Answers