- Home /
Moving Platform Moves instantly.
This is the specific part of the movement in the script,
function movenow(){
yield WaitForSeconds(DelayTime);
transform.position = Vector3.Lerp(position1.transform.position, position2.transform.position, moveTime);
yield WaitForSeconds(pausetime);
transform.position = Vector3.Lerp(position2.transform.position, position1.transform.position, moveTime);
hasnowmoved = false;
ison = false;
}
movetime is set to 1, so I expected the object to move from position1 to p[osition2 in one second, but does it instantly. I'm not sure if I have codedit wrong, or if it's missing something, any help would be appreciated.
Answer by S_Darkwell · Mar 14, 2015 at 10:01 PM
Vector3.Lerp smoothly transitions from first Vector3 to the second Vector3 as its third argument (moveTime) ranges from 0 to 1. Setting moveTime to 1 essentially tells Vector3.Lerp to return a value exactly equal to the second Vector3, which is your destination.
What you want to do instead is to begin with moveTime == 0, then add Time.deltaTime to it during Update(). This will cause it to move over the course of one second.
If you want it to take two seconds, you'd want to change the third argument to moveTime / 2f.
Lerp is like saying "tell me the point which is X% between this start and this end." So if you do something like Vector3.Lerp(v1, v2, 0.5f)
you get the halfway point; if you do Vector3.Lerp(v1, v2, 0.25f)
you get a quarter of the way from v1
to v2
. Therefore, like @Arismeir said, you should be updating the position
during every Update()
call, using Lerp
to deter$$anonymous$$e what you should be updating the value to. You should also investigate $$anonymous$$athf.$$anonymous$$in()
and $$anonymous$$athf.$$anonymous$$ax()
so that you can ensure that your interpolation completes exactly where you intended it to.
For future use by probably other people looking for the same question, this is the script in the door,
var ison : boolean = false;
var direction : int = 1;
var position1 : GameObject;
var position2 : GameObject;
var moveTime : float = 0;
var journeyTime : float = 1;
function Update () {
if(ison == true){
moveTime += Time.deltaTime;
STOPRINE();
if(direction == 1){
transform.position = Vector3.Lerp(position1.transform.position, position2.transform.position, moveTime / journeyTime);
}
if(direction == 2){
transform.position = Vector3.Lerp(position1.transform.position, position2.transform.position, moveTime / journeyTime);
}
}
}
function STOPRINE(){
yield WaitForSeconds(journeyTime);
ison = false;
moveTime = 0;
if(direction == 1){
direction = 2;
}else if(direction == 2){
direction = 1;
}
}
The door will ativate when 'ison' is true, the way 'ison' will be activated depends on what the user will use thew door for.
Yikes - take that code away! Put it in a Wiki or a PasteBin or something, then add a link to it ins$$anonymous$$d.
An alternative to Vector3.Lerp that can sometimes be easier to use is Vector3.$$anonymous$$oveTowards. Like Lerp, it accepts a start and an end Vector3, but ins$$anonymous$$d of the third argument accepting 0 to 1, it accepts a "step-size" float.
So, ins$$anonymous$$d of saying, "Create a Vector3 this percentage between A and B", you are saying "Create a Vector3 this distance toward A from B".
$$anonymous$$oveTowards often helps clean up my movement code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613190139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't get platform to move to the other direction 2 Answers
Player on moving plattform 0 Answers
How to use Lerp? 3 Answers
How to Lerp or Slerp Time.timeScale change 1 Answer
Trying to set up a lerp position system for the camera and failing... 2 Answers