Hi
How to create this action?
I want move object by click on button or a cube.
After click move object leaner from origin to destination.
How to use Lerp or other command?
Hi
How to create this action?
I want move object by click on button or a cube.
After click move object leaner from origin to destination.
How to use Lerp or other command?
If you don’t need to worry about collisions, the best way is to use Lerp in a coroutine, like this:
var object: Transform; // drag the object to move here var destination: Transform; // place an empty object at destination and drag it here var duration: float = 5; // duration; must be > 0! function OnGUI(){ if (GUI.Button(Rect(10,10,120,40),"Move")){ // if button Move pressed MoveObjTo(object, destination.position, duration); // do the movement } } private var moving: boolean = false; function MoveObjTo(obj: Transform, dest: Vector3, time: float){ if (moving) return; // ignore other calls while moving moving = true; // signals it's moving var pointA = obj.position; var t: float = 0; while (t < 1){ t += Time.deltaTime / time; // sweeps from 0 to 1 in time seconds obj.position = Vector3.Lerp(pointA, dest, t); // set position proportional to t yield; // leave the routine and return here in the next frame } moving = false; // finished moving }
EDITED: Fixed 2 fool errors in the first version.