Hey all,
i’d like to move an Object with a function looking like this:
var currentPos : Position;
function moveObject (target : Position){
var speed = 5;
//move the object from its current position to the "target" position with a certain "speed"
}
Call this function in the Update() function and alter the gameobject.transform.position value then inside moveObject(). Like this:
function moveObject (target : Position){
var speed = 5;
//move the object from its current position to the "target" position with a certain "speed"
transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y + speed * Time.deltaTime, transform.position.z + speed * Time.deltaTime)
}[FONT=Helvetica]
[/FONT]
By using Time.deltaTime this object will move with the same speed on any device.
This is actually a very easy solution in Unity using coroutines. You could accomplish it without coroutines as well if needed. Here is pseudo code for how I would do it:
Invoke a new coroutine method and pass that method the “speed” variable and your desired position.
Calculate the distance between your current position and your desired position.
Divide this distance by the “speed” you would like to use for travel, assuming your speed is in units per second.
Divide 1 by the resulting number in the previous step, use this to scale your frame time.
Store a “lerp” variable and do a while(lerp < 1) loop
Inside the loop increment the “lerp” variable by the amount of time elapsed since last frame.
Set the current position equal to the original position lerp to desired position.
yield return null to have the loop wait for next update cycle
You can use the new current position each frame as well instead of the original position, this may give you a better “easing” effect. If you want nice easing you should implement a simple easing equation though.
This is just simple pseudo code from memory, you may need to tweak it to get it working the way you want.