Xitech
1
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
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!
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;
}
}