Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to move an object a specific amount ONLY when button is pressed?

Discussion in 'Scripting' started by FloridaManStudio, Feb 23, 2020.

  1. FloridaManStudio

    FloridaManStudio

    Joined:
    Sep 10, 2019
    Posts:
    6
    I've looked through dozens of tutorials and I still can't figure this out.

    I have up, down, left, and right buttons that control the movement of a 3D object.

    When the player clicks on the buttons, the object will move one unit along the X/Y axis.

    I got this to work, however the objects move in one direction forever and doesn't stop when the button isn't clicked! How do I make the object only move once when the button is clicked?

    Code (CSharp):
    1.  public class ClickMove : MonoBehaviour
    2. {
    3.  
    4.     public GameObject cannon;
    5.     public bool pressed = false;
    6.  
    7.  
    8.  
    9. public void HitButtonLeft()
    10.       {
    11.           if (Input.GetMouseButtonDown(0))
    12.           {
    13.               pressed = true;
    14.                   cannon.GetComponent<Rigidbody>().AddForce(-1000.0f, 0, 0);
    15.  
    16.           }
    17.       }
    18.       public void HitButtonRight()
    19.       {
    20.           cannon.GetComponent<Rigidbody>().AddForce(1000.0f, 0, 0);
    21.  
    22.       }
    23. }
    I know I have 1000.0f right now, but 1 was way too slow and you could barely see movement. I also tried adding the "GetMouseButtonDown(0)" to keep the loop from running unless the left click is active?

    I've also found this code everywhere on the web. but for the life of me I cannot figure out how/where to use it:

    Code (CSharp):
    1.         void update();
    2.         {
    3.             transform.position = new Vector3(3 * time.deltatime, 0, 0);
    4.         }
    What is the best way to do this? I do not have a programmers mindset and have been trying to get this to work for days :(

    Thanks
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
  3. FloridaManStudio

    FloridaManStudio

    Joined:
    Sep 10, 2019
    Posts:
    6
  4. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    if u new like me better start with simple learn tutorials sir
     
  5. FloridaManStudio

    FloridaManStudio

    Joined:
    Sep 10, 2019
    Posts:
    6
    I'm new yes, but I haven't found a single tutorial simple enough to understand :(
    Just want to know where I'm going wrong with this.
    I think I have the right idea, just need to figure out how to link these commands to the buttons OR not to use "void Update" since I don't think that can be linked to multiple buttons.
     
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    also is not better also to use translate rather than force if unit position is to be controlled?
     
  7. FloridaManStudio

    FloridaManStudio

    Joined:
    Sep 10, 2019
    Posts:
    6
    I'm not really sure how to use translate. How do I add this setting per button? Do I need a new script for each button?
     
  8. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    i was just wondering, addforce acts in continuous domain, for speed and accelerate as example, as is difficult to predict end position, it is not suitable for movement in unit directions

    for finite positioning is better to use translate
     
  9. FloridaManStudio

    FloridaManStudio

    Joined:
    Sep 10, 2019
    Posts:
    6
    I also tried:

    Code (CSharp):
    1. private void Update()
    2. {
    3.  
    4. transform.position += new Vector3(1 * Time.deltaTime, 0, 0);
    5. }
    And it didn't work! I can't assign a "void Update" to a button!

    Can anyone help me? I am going crazy with this simple issue. I can't believe this is so hard to do!
     
  10. FloridaManStudio

    FloridaManStudio

    Joined:
    Sep 10, 2019
    Posts:
    6
    Is this impossible to do? Should I just give up?
     
  11. Serellyn

    Serellyn

    Joined:
    Sep 30, 2011
    Posts:
    104
    So, are you still trying to figure this out or did you give up? It's really less complicated that you'd think. If you still want to figure this out and I will help you with some small coding examples.
     
  12. Dekka13

    Dekka13

    Joined:
    Feb 1, 2021
    Posts:
    1
    I think the issue is that since you are using a boolean variable to control the movement once it's set to true there is no way to go back to false, at least from the code you showed