- Home /
Vector3.lerp teleporting instead of lerp?
I have some clouds in my game wich i want to move left and right. I have the following code:
private Vector3 startPos;
private Vector3 endPos;
void Start () {
startPos = transform.position;
endPos = new Vector3(startPos.x + 10f,startPos.y,startPos.z);
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Lerp(startPos,endPos,1f);
}
}
For some reason the cloud is not lerping. But teleporting to the destination instead Before I run the game the cloud is positioned here:
after its positioned here:
I know a speed of 1 could be fast but any number isnt working at all. I tried from 0.1-1 What am I doing wrong?
Cheers
Answer by sparkzbarca · Dec 30, 2013 at 09:45 PM
hehe, 1 is as fast as you can possibly get
i'll try to explain lerp to you
lerp has 3 components
a start point for the line
an end point for th line
and a third number which is 0 to 1
this third number can be viewed as this
"given a line with start point X and end point Y, give me the position Z percent distance along that line"
so a Z of 0 is the start, a Z of 1 is the end and a Z of .5 is the dead middle of the line
a speed of even .1 actaully would only take 10 frames to complete which is 1/6th of a second. In this case we actually need a special value.
This value is the time since the last frame, its referenced by typing time.deltatime
the proper way to use lerp in update is this
cloud.transform.position = vector3.lerp(cloud.transform.position, finalposition, speed * time.deltatime);
good luck!
Thank you for your reply, I've already tried adding time.deltatime. Still no good result(I used the unity video as a reference). Somehow the cloud is moving from 5.607-5.509 ish. I've recorded it with Screenr. Please have a look: Screenr video EDIT: I think it has something to do with it being "in the camera" so the positioning is wrong. $$anonymous$$aybe add the position with position local
startpos must update
it must be
void Start() { Startpos = transform.position; }
Void Update() { Startpos = transform.position;
could.transform.postion = vector3.lerp(start,end,speed * time.deltatime)
if you what to do it that way
but
just get rid of start pos and feed lerp transform.position
the thing is if you read my explanation
right now you have a fixed line for lerp
it has start and end fixed and it just keeps fidning the same point.
what you need to do is find a point a small distance along, go to that point and now lerp from this new point with this small line thats why your start position must keep changing It has to be a variable not a constant or you'll keep getting the same point, thats why you move the first half second, it works but then it breaks because you dont update from that new position.
so
GET RID OF START POS
and change it in update to
transform.pos = Vector3.lerp( transform.position, EndPos, speed * time.deltatime);
the key is that transform.position will take care of updating itself so it'll keep your start pos updating properly.
please mark as answered and have a nice day!
Alright here we go:
public double speed;
public GameObject cloud;
//..................
void Update()
{
cloud.transform.position = vector3.lerp(cloud.transform.position, finalposition, speed * time.deltatime);
}
I am not getting it. You use tranform.position
. But the transform of the cloud is changing so it will keep on moving.
Answer by Spinnernicholas · Dec 31, 2013 at 07:20 PM
You could accumulate the last argument:
double lerpProgress = 0d;
Vector3 start;
//...........
void Awake()
{
start = transform.position;
}
//...........
void Update()
{
lerpProgress += speed * time.deltatime;
cloud.transform.position = vector3.lerp(start, finalposition, lerpProgress);
if(lerpProgress >= 1)
{
//Cloud Reached Destination, Do something....
lerpProgress = 0;
}
}
This is separate from your answer because he isn't getting it.
well he got it, he just didn't realize he needed to update I guess technically your solution might work but its quite ugly :P
lol, if I were to do it personally, I would probably wrap it in a enumerator function that returns true when done:
if(lerpToPosition(startPosition, endPosition, totalTime))
{
//done.....
}
$$anonymous$$aybe even extend Transform:
if(transform.lerpTo(position, time))
{
//done.....
}
Your answer
Follow this Question
Related Questions
rotating road gameobject towards mouse position 1 Answer
Alien Swarm Control with Mecanim 0 Answers
C# to Jacascript 1 Answer