- Home /
Click GUI Button and move the object to another set location
I need some help, I want to make my object move to another set xyz location when clicking the button I used transform.Translate but I don't know how the object will stop to the target location .Thanks a lot! here my script:
using UnityEngine; using System.Collections;
public class NextPosition : MonoBehaviour {
public GameObject objectToMove;
public bool move = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(move == true){
objectToMove.transform.Translate (50f,50f,50f);
}
}
void OnGUI(){
if (GUI.Button (new Rect (Screen.width * (.25f), Screen.height * (.8f), Screen.width * (.05f), Screen.height * (.07f)), "Next")) {
Debug.Log("Moving");
move = true;
}
}
}
Answer by Daniken · Jul 06, 2015 at 12:54 PM
You can consider lerping the object between these two locations. Check out the documentation on Lerp: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
Or you could make a coroutine which is called when the button is pressed. I recommend reading the manual on this one before trying it out. It is a very useful feature to understand and master when building games: http://docs.unity3d.com/Manual/Coroutines.html
Your answer
Follow this Question
Related Questions
Character moving forwards before pressing anything 0 Answers
Name of left ctrl? 1 Answer
Unity can't use the script. 0 Answers
Something wrong with my movement script... 3 Answers
Button not working but other buttons are working. HELP 0 Answers