Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Update / Player Movement / Button Press

Discussion in 'Scripting' started by AvGaz, Oct 14, 2016.

  1. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    Probably a simple question, what's the best way to move an object when a UI button is pressed - separate button for left / right (translate left / right not rotate).

    It seems like within an update(), however that would mean it's always moving, not only with button press.

    Thank-you.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Do you want to move one small increment when you press a button, or would you press and hold for constant movement?
     
  3. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    Thanks, press and hold. Ideally addForce kinda thing. I wasn't sure if the OnClick listener approach would affect frame rate.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    OnClick would only occur when you release the button, so that doesn't do what you want.

    You can create a script to act as your button's behavior, which can handle all the events the button has to offer.

    See this example:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class PhysicsMovementButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    5.  
    6.     // define in the inspector which direction to move the player
    7.     [SerializeField] private Vector3 direction;
    8.  
    9.     // example of a reference to your player's movement script
    10.     private Player player;
    11.  
    12.     private bool pressed;
    13.  
    14.     private void Awake() {
    15.         // get the reference somehow
    16.         // or use a public or [SerializeField]private variable and assign in the inspector.
    17.         player = FindGameObjectOfType<Player>();
    18.     }
    19.  
    20.     private void FixedUpdate() {
    21.         if(pressed) {
    22.             player.Move(direction);
    23.         }
    24.     }
    25.  
    26.     public void OnPointerDown(PointerEventData eventData) {
    27.         pressed = true;
    28.     }
    29.  
    30.     public void OnPointerUp(PointerEventData eventData) {
    31.         pressed = false;
    32.     }
    33. }
    what the implementation on the player side might look like:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : MonoBehaviour {
    4.     [SerializeField] private float movementSpeed;
    5.     private Rigidbody rigidbodyComponent;
    6.  
    7.     private void Awake() {
    8.         rigidbodyComponent = GetComponent<Rigidbody>();
    9.     }
    10.  
    11.     public void Move(Vector3 direction) {
    12.         rigidbodyComponent.AddForce(direction * movementSpeed, ForceMode.Impulse);
    13.     }
    14. }
    This would allow your button to add a force to the player every physics update, and then it would be the player's rigidbody drag slowing him down when you let go of the button.
     
  5. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    Excellent, thank-you for the detailed answer.
     
  6. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    How do you get it to go? Attach to the button, or a GameManager? Thanks.
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The "PhysicsMovementButton" script would have to be attached to an object with a Button component in order to receive those events.
     
  8. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    Thanks, I ended up doing it in a way that might be useful to others - in the inspector for the button, add component 'event trigger' and add to it 'on point down' and an 'on point up' - add to them the game manager object and select a method from a script that has a bool, have a method for on point down that sets the bool = true, and another method for on point up which sets the bool to false. Add in this script an update() and add a condition with in it - if bool == true then... which is the script that will run continuously if the button is held down.

    I'd sure this is the same thing with out some of the scripting, but was quite easy to implement.
     
    LiterallyJeff likes this.