How to Move the Player Using UI Buttons in Unity 2D


In the world of game development providing a reflexive and responsive user interface is important for the player experience. One common scenario is moving the player character using UI buttons. In this guide, we will explain a step-by-step process of moving the player and how to achieve this in Unity 2D.



Step 1: 

Begin by right-clicking in the hierarchy -> UI ->Cavas.



Step 2: 

Create the UI buttons that act as the bridge between the player's intentions and the game's response. Right-click on the canvas, and choose UI -> Button - TextMesh Pro. Repeat this process for each directional button you want to implement for example (left, right).



Step 3:

Place the UI buttons on the screen according to your game's design. Adjust their RectTransform properties in the Inspector window too.



Step 4:


Select each button and add an EventTrigger component. Expand the EventTrigger component and add a new event for the desired trigger type (e.g., Pointer Down, Pointer Up). In the Event trigger there is a list of trigger types so choose what you want to use.



Step 5:

Create a C# script for the player movement and attach it to the player GameObject. In the script, create public methods for each direction. 


For example:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class YourScriptName : MonoBehaviour
{
    private Rigidbody2D rb;
    private float horizontalMove;
    private Vector3 initialScale; // Store the initial scale
    public float speed = 8;

    void Start()
    {
        rb = GetComponent Rigidbody2D();

        // Store the initial scale of the GameObject
        initialScale = new Vector3(0.3f, 0.25f, 1);
    }

    public void PointerDownLeft()
    {
        horizontalMove = -speed;

        FlipCharacter(-0.3f); // Flip the character to face left
    }

    public void PointerUpLeft()
    {
        horizontalMove = 0;

    }

    public void PointerDownRight()
    {
        horizontalMove = speed;
        FlipCharacter(0.3f);
    }

    public void PointerUpRight()
    {
        horizontalMove = 0;

    }


    void FixedUpdate()
    {
        // Apply the horizontal movement
        rb.velocity = new Vector2(horizontalMove, rb.velocity.y);
    }

    void FlipCharacter(float direction)
    {
        // Flip the character sprite based on the movement direction
        Vector3 scale = initialScale; // Use the stored initial scale
        scale.x = direction;
        transform.localScale = scale;
    }

}

Step 6: 

In the EventTrigger component of each button, assign the player GameObject as the target. Choose the script and select the appropriate method for the button's direction.







This is a step-by-step guide allows game developers to create responsive controls and UI Buttons, opening new possibilities for game interactions and user engagement. Experiment, and collect these concepts to suit the unique requirements of your game.





Happy coding


Previous Post Next Post

Contact Form