- Home /
 
How to keep an object moving when holding down a button
Hi. I want to have a button that moves an object. The problem is that when the button is clicked, it moves the object only once a little bit and you have to press the button again to move the object again. How can a make it so that while the button is being held down, the object moves and when you let go it stops? Here is my code:
void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 private void OnGUI(){
     if (GUI.Button (new Rect (menu1, menu2, 100, 50), "UP")) {
         transform.Translate(Vector3.up * moveSpeed * (Time.deltaTime)*5);
     }
     if (GUI.Button (new Rect (menu3, menu4, 100, 50), "DOWN")) {
         transform.Translate(-Vector3.up * moveSpeed * (Time.deltaTime)*5);
     }
     if (GUI.Button (new Rect (menu5, menu6, 100, 50), "Left")) {
         transform.Translate(Vector3.left * moveSpeed * (Time.deltaTime)*5);
     }
     if (GUI.Button (new Rect (menu7, menu8, 100, 50), "Right")) {
         transform.Translate(-Vector3.left * moveSpeed * (Time.deltaTime)*5);
     }
     }
 
              I would use the GUI repeat button. Although if it is ios(tell me if it is), then do the touch function. Though anyhow, here is the repeat button doc.
http://docs.unity3d.com/Documentation/ScriptReference/GUI.RepeatButton.html
Answer by J-R-Wood · Dec 31, 2013 at 02:11 AM
 using UnityEngine;
 using System.Collections;
 
 public class MovePlayer : MonoBehaviour
 {
     Public float speed;
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis("Horizontal"); //this code should do perfect if i understand you, it will move exactly when       //you tell it to until you release the key. Please vote my answer up when you find it works
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rigidbody.AddForce(movement * speed * Time.deltaTime); 
     }
 }
 
 
              Answer by sheffieldlad · Dec 31, 2013 at 02:10 AM
use repeatbutton
 private void OnGUI(){
 if (GUI.RepeatButton (new Rect (menu1, menu2, 100, 50), "UP")) {
 transform.Translate(Vector3.up * moveSpeed * (Time.deltaTime)*5);
 }
  
 if (GUI.RepeatButton (new Rect (menu3, menu4, 100, 50), "DOWN")) {
 transform.Translate(-Vector3.up * moveSpeed * (Time.deltaTime)*5);
 }
  
 if (GUI.RepeatButton (new Rect (menu5, menu6, 100, 50), "Left")) {
 transform.Translate(Vector3.left * moveSpeed * (Time.deltaTime)*5);
 }
  
 if (GUI.RepeatButton (new Rect (menu7, menu8, 100, 50), "Right")) {
 transform.Translate(-Vector3.left * moveSpeed * (Time.deltaTime)*5);
 }
 
              Your answer
 
             Follow this Question
Related Questions
Moving an instantiate object with buttons 1 Answer
How to transpose key press events to UI Buttons 1 Answer
Click Goes Everywhere 0 Answers
Button input getting stuck 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers