- Home /
Simplest movement to point possible
I have a cube, on a simple plane..
The cube is at position 5.6.7.
i have an update function in javascript.
What is the easiest way to move the cube to position 2,3,9 over a period of 2 seconds?
Okay, to answer myself, I used the moveobject script from the unifycommunity wiki...this is the easiest way i could find...
Answer by whydoidoit · Jun 17, 2012 at 10:52 AM
var time =0.0;
function Update() {
time += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, Vector3(2,3,9), time / 2);
}
You would need to reset time to 0 if you move the object and wanted the movement to happen again.
This makes it takes steps to the location right? Is there a way to just set it and forget or to know when it has reached its destination?
The way to move an object in a realtime engine is to go step by step during each frame. So no, "fire and forget" is never possible. But you could set up a callback system that sends an event when the start and end vectors are identical/within tolerance, and have objects that want to be notified subscribe to the callback. It's not clear whether you're solving for that setup.
You could just use iTween - it's a free download from the asset store. (It does what I'm suggesting you do, but hides it from your code).
Answer by Drakestar · Jun 17, 2012 at 10:53 AM
From the documentation:
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}