- Home /
Moving elevator based on specified time limit
I'm trying to make an elevator move to a position based on the amount of time that's on an in-game clock. It's supposed to work similarly to a survival section in some games, where an elevator lift takes a specified amount of time to reach the bottom of a shaft.
As an example, I'll use a time limit of 30 seconds. If 5 seconds have passed, the platform will be 1/6 the way toward its final position; if 15 seconds pass, it will be halfway; after 30 seconds, the platform should be at its final destination.
I've tried setting the speed of the elevator's descent based on time rather than frames, but the speed is not consistent. Simply setting the speed to "(time limit) * Time.deltaTime" makes run faster the larger the time limit is; using the same equation but dividing the time limit by 60 (which I assumed would aid in making the time limit based on seconds) doesn't seem to work, either.
Any pointers on moving an elevator based on a specified time limit would be much appreciated.
you could use lerp to put it simply. Check the docs to see how it works. It scales with respect to time. From point A to point B
Answer by Seth-Bergman · Aug 11, 2013 at 06:35 AM
basically you could say something like:
var start : Vector3;
var end : Vector3;
var duration : int = 60;
private var startTime: float;
function Start(){
start = GameObject.Find("Start Point").transform.position;
end = GameObject.Find("End Point").transform.position;
startTime = Time.time;
}
function Update(){
var pos = (Time.time - startTime) / duration;
transform.position = Vector3.Lerp(start,end,pos);
}
something like this should do it (just tested, this works as desired)
Unfortunately, the elevator travels too quickly for me to use as I intended. I did not specify this earlier, but I'm also using $$anonymous$$oveTowards() rather than Lerp(), as the latter tends to give me problems when moving one object to another position.
I just realized that by using Time.deltaTime, I am actually specifying how far an object will travel per second. I could use that to find out how long it would take to travel a specified amount of meters. Then, I could use that to ensure the elevator only travels at the correct speed to get to its destination by the end of the time limit.
my script above works, just change "duration" to a larger number to decrease the speed.. notice the line:
var pos = (Time.time - startTime) / duration;
(Time.time - startTime) is the elapsed time so far.. duration is the total time in seconds that the elevator will take to reach the end point.. Now, let's say we get the distance between start and end-point, that would simply be divided by duration to deter$$anonymous$$e the meters-per-second.. for example, if the total distance is 1000 meters, and the duration is 60, we know the speed of the elevator will be 1000/60, or 16 2/3 meters-per-second.
Your answer
Follow this Question
Related Questions
Spawn Randomly over Time 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers
Take damage after seconds 3 Answers
Pause game while GUI instructions displayed? 1 Answer
Problem with elevator script (C#) 1 Answer