0
\$\begingroup\$

I'm trying to move the player based on UI buttons. I found this topic which covers the button listener: https://forum.unity.com/threads/touch-and-hold-a-button-on-new-ui.266065/#post-1944748

Now I know when the button is being pressed, but how can I "connect" those two scripts to make the player move?

I have this script to the player which was moving without using button.

public class player : MonoBehaviour {
     Rigidbody m_Rigidbody;
     public float m_Speed;

     // Use this for initialization
     void Start () {                
         m_Rigidbody = GetComponent<Rigidbody>();
         //Set the speed of the GameObject    
     }

     // Update is called once per frame
     void Update () {
         /* OLD -> WITHOUT BUTTON

         float x = Input.GetAxis("Horizontal");
         float y = Input.GetAxis("Vertical");

         x = -x;

         m_Rigidbody.velocity = new Vector2( x * m_Speed,  y * m_Speed);
         */
     }
 }

 public class scrLeftButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

     // Use this for initialization
     void Start () {        

     }        

     // Update is called once per frame
     void Update () {
         if (!isPressed) {
             return;
         }
     }

     bool isPressed = false;

     public void OnPointerDown(PointerEventData eventData){
         isPressed = true;
     }

     public void OnPointerUp(PointerEventData eventData){
         isPressed = false;
     }
 }
\$\endgroup\$
1
\$\begingroup\$
// Class names should have their first letter capitalized
public class Player : MonoBehaviour
{
     public float movementSpeed;
     private Rigidbody rigidbody;
     public PlayerButton LeftButton; // Drag & drop the left button object
     public PlayerButton RightButton; // Drag & drop the right button object;
     public PlayerButton UpButton; // Drag & drop the up button object;
     public PlayerButton DownButton; // Drag & drop the down button object;

     // Use this for initialization
     void Start ()
     {                
         rigidbody = GetComponent<Rigidbody>();
     }

     // FixedUpdate must be used when you manipulate the physics components
     void FixedUpdate ()
     {
         Vector2 velocity = Vector2.zero ;
         if( RightButton.IsPressed ) velocity.x += movementSpeed ;
         if( LeftButton.IsPressed )  velocity.x -= movementSpeed ;
         if( UpButton.IsPressed )    velocity.y += movementSpeed ;
         if( DownButton.IsPressed )  velocity.y -= movementSpeed ;

        rigidbody.velocity = velocity * Time.fixedDeltaTime;
     }

 // Attach this script to your 4 buttons.
 public class PlayerButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
 {
     // Define a property so that other classes can know whether the button is pressed
     public bool IsPressed
     {
         get ;
         private set ;
     }

     public void OnPointerDown(PointerEventData eventData)
     {
         IsPressed = true;
     }

     public void OnPointerUp(PointerEventData eventData)
     {
         IsPressed = false;
     }

     public void OnPointerExit(PointerEventData eventData)
     {
         IsPressed = false;
     }
 }
\$\endgroup\$
2
  • \$\begingroup\$ It worked! The only thing i had to change was the return type of IsPressed method to bool. Thank you! \$\endgroup\$
    – ziad.ali
    Sep 13 '18 at 14:58
  • \$\begingroup\$ My bad, I fixed the answer. Thanks for your feedback ! \$\endgroup\$
    – Hellium
    Sep 13 '18 at 14:59
0
\$\begingroup\$

This is your old way of moving the character:

// Update is called once per frame
void Update () {
     float x = Input.GetAxis("Horizontal");
     float y = Input.GetAxis("Vertical");

     x = -x;

     m_Rigidbody.velocity = new Vector2( x * m_Speed,  y * m_Speed);
 }

If you want to replace that with UI buttons, simply set the value of x and y based on the buttons. For example, when the right arrow is pressed, you could set x to 1.0 and for left arrow -1.0. Do the same for y.

One way to do this, is to give a reference of each button to the player, then you could do something like:

// Initialise to no button pressed
float x = 0;

// If a button is pressed, change x's value
if (myLeftArrow.IsPressed())
    x = -1;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.