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…

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();
    }
}

Either way you are going to need Update() somewhere :stuck_out_tongue:

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;
    }
}

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 :slight_smile:
Bye