- Home /
Need Update without... function Update
Hi,
I'm using unity from 5 days and I have some big difficulty with scripts, because I never used Java, so I try to ask here... maybe you can help me...
In the scene there are a Sphere and a Cube. The Sphere must move to Cube when I push a button.
This is the script:
private var speed = 0.25;
private var increment : float;
function OnGUI () {
// Build a Box
GUI.Box (Rect (10,10,100,90), "Move");
// Build the button. If push goto the function Move
if (GUI.Button (Rect (20,40,80,20), "Cube")) {
MoveSphere ();
}
}
function MoveSphere () {
// Look if the Sphere has a different position than Cube
if (transform.position != GameObject.Find("Cube").transform.position) {
if (increment <=1) {
increment += speed/100;
}
// Move the Sphere to the Cube position
transform.position = Vector3.Lerp(transform.position, GameObject.Find("Cube").transform.position, increment/2);
}
else {
print ("Finish!");
increment = speed/100;
return;
}
}
The first function creates a box and a button, if pushed begin the second function.
The problem is that the Sphere moves only a little distance, because need to repeat the function MoveSphere until the Sphere get the Cube position. I tried to write an other call to function MoveSphere (); after the transform.position=Vector3.Lerp etc. etc. but the execution is so fast than all the cycle of move happens in less time than a frame refresh, so the Sphere goes to Cube in 1 frame only. If I change the function MoveSphere () with the function Update () the Sphere moves right, like I want, but all begins when the script run and not when I push the button... and the function can't stop with return command... so the function continue to calculate the same transform.position forever.
How can I repeat the function MoveSphere every frame (or every 1/30 seconds) like happen with function Update ?
Thankyou very much for any help and really sorry for my bad english...
Answer by Byterunner · May 24, 2011 at 04:37 PM
Declare a variable that holds the value of whether the sphere should be moving or not. When you click the button, make that value true. In the Update function, test whether that value is true and if it is, move the sphere.
In addition to what you already have, add the following (replace your OnGUI).
private var moving = false;
function OnGUI () {
// Build a Box
GUI.Box (Rect (10,10,100,90), "Move");
// Build the button. If push goto the function Move
if (GUI.Button (Rect (20,40,80,20), "Cube")) {
moving = true;
}
}
function Update() {
if (moving) {
MoveSphere();
}
}
Answer by Meltdown · May 24, 2011 at 05:01 PM
Either way you are going to need Update() somewhere :p
I modified your script, and did the following, instead of increment in Lerp I used Time.deltaTime to smoothly move the sphere over to the cube...
var moving : boolean = false;
var increment : float;
var speed : float = 0.25;
function Update()
{
moving = true;
if(moving)
MoveSphere();
}
function MoveSphere() {
// Look if the Sphere has a different position than Cube
if (transform.position != GameObject.Find("Cube").transform.position) {
if (increment <= 1) {
increment += speed;
}
transform.position = Vector3.Lerp(transform.position, GameObject.Find("Cube").transform.position, Time.deltaTime / 2);
}
else {
print("Finish!");
increment = speed / 100;
return;
}
}
Sorry to forgot to mention, you can set the variable moving = true in your OnGUI() method when clicking the button as Byterunner mentioned.
Answer by Gianluca · May 24, 2011 at 05:32 PM
Really thankyou Byterunner and Meltdown! With this condition inside Update it works like I want!!!
For Meltown: I don't use Time.deltaTime because when the Sphere is more and more near to cube it goes more and more slow and need to much time to arrive... so it is fast in begin but too slow at the end. But maybe need some operation with deltatime and I can tune the velocity.
Thankyou for now :) Bye
Cool, remember to vote on an answer if it helps you and mark as answer the question that best answered your question. Also use comments on people's posts to reply to their answer, don't create a new 'answer' :p
Your answer

Follow this Question
Related Questions
i want my object to move when it collides with my player 2 Answers
Block Selecting and Moving 0 Answers
How to do that? 0 Answers
Why is my object not moving 1 Answer