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);
}
}
}
Thanks for your question, but you wouldn’t be asking it if you looked hard enough
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.
“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…