- Home /
Slide GUITexture/GameObject into view?
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);
}
}
Answer by robertbu · Apr 14, 2014 at 06:17 PM
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.
Your answer
Follow this Question
Related Questions
How to move two same Objects at the same place in different scenes? 1 Answer
Take final position after a force applied to a GameObject 1 Answer
Moving a transform behaves odd 0 Answers
Need insight on strange (simple) 2D object transform bug 0 Answers
Can i transform the position of an object to another object? 1 Answer