gameobject translation problem

Hi

I haved created a button that can create a random number.I want to make a game that after I press the button, my gameobject moves units with the random number. But the script that I write let my gameobject move suddenly. This is not what I want. I want my gameobject moves smoothly and slowly. Please tell me how to modefy.:frowning:

var label:String;
var num:int;
var speed:float = 10.0;


function ChangeNumber(){
		num=UnityEngine.Random.Range(1, 6);
		label=num.ToString(); 
		Move();
	}

function OnGUI(){
		if(GUI.Button(new Rect(20, 240, 50, 50), label))
			ChangeNumber();
	}

function Move(){
		transform.Translate(Vector3(speed,0,0) * num * Time.deltaTime);
	}

Thanks:)

Do you drag this code on your gameObject?
There isn’t a function to call your function what you have writen.

You’re going to want to use Lerp I imagine :slight_smile:

var label:String;
var num:int;
var speed:float = 10.0;
var targetPosition:Vector3; 
 
function Start() {

targetPosition = transform.position;

}

function ChangeNumber(){
       num=UnityEngine.Random.Range(1, 6);
       label=num.ToString(); 
       targetPosition = Vector3(num, transform.position.y, transform.position.z);
    }
 
function OnGUI(){
       if(GUI.Button(new Rect(20, 240, 50, 50), label))
         ChangeNumber();
    }
 
function Update(){
       transform.position = Vector3.Lerp(transform.position, targetPosition, speed*Time.deltaTime);
    }

Simply change the speed value in the inspector for varying speeds of smooth movement!

Oh and another thing, if you want it to move an additional amount of units based on its currrent location just change:

targetPosition = Vector3(num, transform.position.y, transform.position.z);

to:

targetPosition = Vector3(transform.position.x + num, transform.position.y, transform.position.z);