Hi there,
What i’m looking to happen is when I press ‘i’ the gameobject will scroll from (0,-0.5,0) to (0,0,0) and stop.
I have found a way to do it, but I don’t know if it’s the best way. What are other / best ways of doing this?
What works:
function Update ()
{
if (Input.GetKeyDown("i"))
{
slide();
}
}
function slide ()
{
while (transform.position.y < 0)
{
transform.position = transform.position + new Vector3 (0, 0.01, 0);
yield WaitForSeconds(0.00001);
}
}
There’s lots of different ways of doing most things. So better and best is relative to your game mechanic. For example, the solution I by implement would be different for a one-time movement vs. moving it around multiple times. I see some potential issues in your code, but it should work. Here is an alternative:
#pragma strict
var dest : Vector3 = Vector3.zero;
var speed = 0.5; // Units per second
private var sliding = false;
function Update () {
if (!sliding && Input.GetKeyDown(KeyCode.I)) {
slide();
}
}
function slide () {
sliding = true;
while (transform.position != dest) {
transform.position = Vector3.MoveTowards(transform.position, dest, speed * Time.deltaTime);
yield;
}
sliding = false;
}
You might also want to take a look at the MoveObject script in the Unity Wiki for another idea on how you might want to implement this functionality.