0
\$\begingroup\$

i have been using transform.positionsince the start but now i want to switch to rigidbody instead since i am facing some collision problems. i can use the keyboard to move the player using this code:

input = new Vector3 (Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));  

        if (GetComponent<Rigidbody> ().velocity.magnitude < maxmovespeed) {
            GetComponent<Rigidbody> ().AddForce (input * movespeed);
        }

but since i am building for mobile and have added UI buttons for right/left on screen, i wish to use those for moving my player. how can i use those buttons to move in that specific direction like left for left only and right for right?

\$\endgroup\$
1
\$\begingroup\$

If you only need to move left and right, you could try something like this:

if (GetComponent<Rigidbody> ().velocity.magnitude < maxmovespeed) {
    if (leftButton.IsPressed()) {
        GetComponent<Rigidbody> ().AddForce (-Vector3.right * movespeed);
    } else if (rightButton.IsPressed()) {
        GetComponent<Rigidbody> ().AddForce (Vector3.right * movespeed);
    }
}

Just assign leftButton and rightButton as public variables in the editor.

\$\endgroup\$
2
  • \$\begingroup\$ how can i assign them as public variables? \$\endgroup\$ – Mohit Saxena Dec 30 '16 at 17:45
  • 1
    \$\begingroup\$ Add instance fields in the script, like "public UnityEngine.UI.Button leftButton", then in the editor drag and drop the UI element onto that variable. \$\endgroup\$ – wenright Dec 30 '16 at 22:28
0
\$\begingroup\$

We use two Event triggers and Delegate for one is Left side button and second is Right side button.

public class EventManager : MonoBehaviour
{
    public delegate void OnMoveLeft ();
    public static event OnMoveLeft onMoveLeft;
    public static void RaiseOnMoveLeft ()
    {
        if (onMoveLeft != null)
            onMoveLeft ();
    }

    public delegate void OnMoveRight ();
    public static event OnMoveRight onMoveRight;
    public static void RaiseOnMoveRight ()
    {
        if (onMoveRight != null)
            onMoveRight ();
    }
}

When UI button clicked, same time event must raise either Left Side or Right Side.

public class GamePlayView : MonoBehaviour
{
    public void OnLeft(){
        EventManager.RaiseOnMoveLeft();
    }

    public void OnRight(){
        EventManager.RaiseOnMoveRight();
    }
}

Player Script We have to use Event Register and Deregister for Both Event triggers. When Button click that time according to that Event handlers method are called. And player performed.

public class Player : MonoBehaviour
{
    public float forwardSpeed;
    private Rigidbody rigidbody;

    void Awake(){
        rigidbody = GetComponent<Rigidbody>();
    }

    void OnEnable(){
        EventManager.onMoveLeft += EventManager_onMoveLeft;
        EventManager.onMoveRight += EventManager_onMoveRight;
    }

    void OnDisable(){
        EventManager.onMoveLeft -= EventManager_onMoveLeft;
        EventManager.onMoveRight -= EventManager_onMoveRight;
    }

    void EventManager_onMoveLeft ()
    {
        rigidbody.AddForce(-Vector3.right * forwardSpeed);
    }

    void EventManager_onMoveRight ()
    {
        rigidbody.AddForce(Vector3.right * forwardSpeed);
    }
}
\$\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.