Touch movement in a 2d game

Hello guys, I wanna make my game compatible with mobile devices, but I don’t know how to set it up in my code. Currently I have just keyboard controlls. the mobile input should there instead of the keyboard inputs

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using TMPro;

public class Player : MonoBehaviour
{
    [SerializeField] SpriteRenderer playerImage;
    [SerializeField] TMP_Text playerNameText;

    public float speed = 1f;

    Rigidbody2D rb;
    bool isMoving = false;
    float x, y;
  




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

        ChangePlayerSkin();
    }

    void ChangePlayerSkin()
    {
        Character character = GameDataManager.GetSelectedCharacter();
        if (character.image != null)
        {
            playerImage.sprite = character.image;
            playerNameText.text = character.name;

        }
    }

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

        isMoving = (x != 0f || y != 0f);
    }

    void FixedUpdate()
    {
        if (isMoving)
        {
            rb.position += new Vector2(x, y) * speed * Time.fixedDeltaTime;
         
        }
    }

    void OnCollisionEnter2D (Collision2D other)
    {
        string tag = other.collider.tag;

        if (tag.Equals ("Coins"))
        {
            GameDataManager.AddCoins(25);

            GameSharedUI.Instance.UpdateCoinsUIText();

            Destroy(other.gameObject);
        }
    }
}

This is definitely a google-for-it thing. :slight_smile:

Alternatively you’re welcome to look at my open-source proximity buttons project to see how I did it there:

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

Thank you for your answer but I would not be here if i found something on google :slight_smile:

Thanks for your question, but you wouldn’t be asking it if you looked hard enough :slight_smile:

I used to spend weeks on a problem until I came up with a solution on my own. Never asked anything on here until I was a few years in. Don’t waste the time of others to solve your problems, learn on your own, ask only if you absolutely must.

Indeed… or get a private tutor. It’s not practical for volunteers (us!) to sit here and type in the essentials of how to do something where there are 57,000 different people who have made actual video and solved the problem for you on Youtube, and that video is available at zero cost anywhere night or day in the internet-connected world.

Any one else here who dose not blame me for asking? :frowning:

“Currently I have just keyboard controlls. the mobile input should there instead of the keyboard inputs”

You didn’t specify what kind of game you are making.

Are you going to create touch buttons so your game works pretty much like when using physical buttons?
Or are you aiming to create some sort of touch to move system?
Or haven’t you decided this yet? If so, it is pretty hard to help.

But anyway, so ideas:

Steps for buttons:

  • Create screen space overlay Canvas
  • Add buttons
  • Connect buttons to your methods or do the same from code to use buttons

Steps for touch

  • Get user touches using Unity’s Touch class
  • Each Update see if you have touches, and what the Phase of the touch is (Began, Moved, Ended) and react accordingly

Tutorials - these keywords will find you some good examples:
“unity touch buttons”
“unity touch to move object”

lol what is wrong with asking, if you feel this is a waste of your time then you better spend your time somewhere else. no one forced you to read his question…

Please don’t necro-post. Make your own post and describe your problem correctly. Here’s how:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379