- Home /
Smoothly transform gameobject to specific coordinates with the click of a GUI
I looked around the internet for a way to do this. I found a lot of things very similar but not quite what i want it to be. I want to make a target have 3 distances, close middle and far. And I have the GUI set up and the variables set up #pragma strict var Target : GameObject; var Speed = 20;
function OnGUI () { if (GUI.Button(Rect(30,40,180,40), "Put the target in the front")) {
}
if (GUI.Button(Rect(30,90,180,40),"Put the target in the middle" )){
}
if (GUI.Button(Rect(30,140,180,40),"Put the target in the back" )){
}
} I just got to get something to put in the gui click functions which makes the object smoothly go to specific coordinates, with the click of a button, and i want it so if the same button is clicked more than once than it won't do anything.
Answer by KurtGokhan · Jan 30, 2014 at 09:11 AM
Something like this might work for you:
var targetPos : Vector3;
//You will set targetPos with button click.
function Update(){
Target.transform.position = Vector3.Lerp(Target.transform.position,targetPos,Time.deltaTime * Speed);
}
Change Speed to suit your needs.
You can not Vector3 lerp a transform. Use transform.position (which is a Vector3).
I put the variable at the top like usual, then i put the function in where the button function is. But like i said i kinda want to have specific coordinates, and i don't know how to do that
if (GUI.Button(Rect(30,140,180,40),"Put the target in the back" )){
targetPos = Vector3(x,y,z); // x,y,z are number you will deter$$anonymous$$e
}
Something like this should work for you.
Thanks! But i have 2 more noob questions haha. How do i make it go slower, and for some reason when i start the game the target in question gets teleported to 0,0,0. I assume this is because that is what vector 3 is at the start of the game before any buttons is pushed.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
I get a weird error when i try to use GUISkin on a timer 1 Answer
Moving gameobject with script isn't working 0 Answers
Restart Level not working... 1 Answer
Can't move GUI 1 Answer