Home > Mobile >  How To Add Mobile Touch Controllers In Unity
How To Add Mobile Touch Controllers In Unity

Time:12-05

I am working on a game, and till now, i have allowed a user to play the game by moving up and down using the up and down arrow keys on the computer keyboard.

But now i want to make the user be able to do that on a mobile phone by touching up or down, or maybe swiping up or down to move in the game.

I am using c# for this.

This is the current code assigned to the player object:

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

public class Player : MonoBehaviour
{
    public float playerSpeed;
    private Rigidbody2D rb;
    private Vector2 playerDirection;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float directionY = Input.GetAxisRaw("Vertical");
        playerDirection = new Vector2(0, directionY).normalized;
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2(0, playerDirection.y * playerSpeed);
    }
}

What do i need to do to achieve this?

CodePudding user response:

You should try to make buttons with trasparency

CodePudding user response:

You have plenty of possibilities here ! But there might have better solutions than others ;)

You could make a UI representing up and down arrow that respond with wathever behaviour you'd like when pressed
OR
you could dig a little the Input documentation, espacially what's relative to touches.

From there you can get touch position in screen space an make whatever you want with it !

The choice is lead by the type of game or application you're trying to make. Hope that helped ;)

  • Related