- Home /
Lerping using Time.deltaTime results in object going to origin.
I've been working on this all day and can simply not find what the problem is, I've looked all over unityAnswers and have not found a working solution, though I seem to be getting closer. Whenever I use a the following my object will move down to the correct position and then at the end suddenly jump to the origin. It's the most infuriating thing. I've done some prints to try to figure out what exactly happens and everything works until the very end. After that I can no longer move it down and it moves up to the wrong place. It's so nutty.
if(Input.GetButtonDown("up") && transform.position.y < 40 && !movingUp)
{
var targetPosY : Vector3 = Vector3(transform.position.x, transform.position.y + 40, transform.position.z);
movingUp = true;
print("on up" + targetPosY);
}
if(movingUp && transform.position.y < targetPosY.y)
{
MoveObject(transform.position,targetPosY, 0.5f);
if(transform.position.y >= targetPosY.y)
{
//transform.position.y = targetPosY.y;
movingUp = false;
}
//print("upB" + movingUp);
}
if(Input.GetButtonDown("down") && transform.position.y > -70 && !movingDown)
{
//var currentNegY : Vector3= Vector3(transform.position.x, transform.position.y, transform.position.z);
var targetNegY : Vector3 = Vector3(transform.position.x, transform.position.y - 40, transform.position.z);
movingDown = true;
print("on down" + targetNegY);
}
if(movingDown)
{
MoveObject(transform.position,targetNegY, 0.5f);
//transform.Translate(Vector3.down * 1);
if(transform.position.y <= targetNegY.y)
{
transform.position.y = targetNegY.y;
movingDown = false;
}
//print("downB" + movingDown);
print("on down" + targetNegY);
}
function MoveObject (startPos : Vector3, endPos : Vector3, time : float) {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0)
{
i += Time.deltaTime * rate;
transform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
Sorry about the trashy code, I've tried so many things in an attempt to get this to work. Help is greatly appreciated!
Answer by aldonaletto · Sep 04, 2013 at 01:44 AM
Well, I couldn't understand what exactly you wanna do, but suppose that you just want to move up and down in steps of 40 units. From your code, I suspect that you're starting multiple MoveObject coroutines - this probably would produce the weird behaviour you've described.
To avoid this, use a boolean flag moving : set it when MoveObject starts and clear it when the loop finishes, and only check the keyboard when moving is false:
private var moving = false;
function Update(){
if (!moving){ // if not currently moving...
if (Input.GetButtonDown("up") && transform.position.y < 40){
var targetPosY : Vector3 = transform.position + 40 * Vector3.up;
MoveObject(transform.position,targetPosY, 0.5f); // move 40 up
}
if (Input.GetButtonDown("down") && transform.position.y > -70){
var targetNegY : Vector3 = transform.position - 40 * Vector3.up;
MoveObject(transform.position,targetNegY, 0.5f); // move 40 down
}
}
}
function MoveObject (startPos : Vector3, endPos : Vector3, time : float){
moving = true; // I'm moving - don't call me until the movement is done
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0){
i += Time.deltaTime * rate;
transform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
moving = false; // movement done - you can call me again
}
While this is 100% better written than $$anonymous$$e and I def appreciate the effort for that it's still having the same problem. This makes it move down over a short time towards the world space origin, ins$$anonymous$$d of simply negative 40 units on the Y axis, when I press the down key, in this case "s". Important to note: this doesn't happen when I make it go up. It simply goes up 40 units on the Y axis from wherever it's at the time.
Okay, checked the code again and there was just a small typo which I left in there because I copy pasted the code. It works now, thank you thank you thank you. I was beating my head on this all day.
Yes, there was a typo in the down if - I used targetPosY ins$$anonymous$$d of targetNegY. Answer fixed now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113526im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
camera Lerp Silly Question 3 Answers
When enemy health is 0, the player gets 73 XP, about 500,000 times. 2 Answers
Vector3 Lerp using Time.deltaTime? 3 Answers