Moving Player Left/Right with Buttons

I am creating a 2D Space Invaders game and I want to be able to move the player’s ship side to side using “Left”/“Right” buttons (UI) i.e. run ‘this’ method as long as ‘this’ button is pressed.

I have also tried adding my buttons to input manager but I don’t think it worked.

I have already created and placed the buttons but the script I attached to them does not work at all, including the debug print statement:

private PlayerController playerController;
  
void Start ()
{
    playerController = GameObject.Find("Player").GetComponent<PlayerController>();
}

void Update ()
{
    if (Input.GetButtonDown ("Right"))
    {
        print ("RIGHT CLICKED");
        playerController.transform.position += Vector3.right * 15.0f * Time.deltaTime;
    }
}

Any and all help is appreciated :slight_smile:

I think you need GetButton()

Returns true while the virtual button identified by buttonName is held down.

GetButtonDown()

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

so change line 12 to

if (Input.GetButton ("Right")) {

hi;
by buttons u mean buttons in the game ?
or keys in the keyboard?

if number 1 is your problem then u cant do it like that u need to make it as a seperate function and call it when the button is pressed ( not put it in update);

send a screen shot so i can help u better;

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 → UICanvas.

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 UIButton - 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.

c

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.

Source: