Hello everyone! I am trying to make my game work on android phone. I need to switch from my keyboard input to screen touch input. I am using Input.GetAxis() usually, when the input is my keyboard arrows. I want to replace that keyboard button(s) of axis with particular button(s) of my ui elements, so that when I touch my screen, it will be the same as I would push the keyboard button. I want to do that, because axis settings has lots of settings like ‘sensitivity’ or ‘gravity’ and etc. and so I would not need to make that input system by myself, only replace the input button. So how can I do that?
Thanks.
Simple solution:
You can try using the following code , for replicating the push of a keyboard button or mouse button…
float xPos = Input.GetAxis("Mouse X");
float yPos = Input.GetAxis("Mouse Y");
if (Input.touchCount > 0)
{
xPos = Input.touches[0].deltaPosition.x;
yPos = Input.touches[0].deltaPosition.y;
}
Heres info on how mobile input works:
Recommend solution
Although, I would recommend that if you are using the latest Unity UI .
-
Create a button , and add an event trigger component
-
Add two new events ( on the event trigger component) from the options select “Pointerdown” and “PointerUp”.
These events will be triggered when you push this button on the UI and release it , respectively. -
Hook corresponding functions like “OnPointerDown” etc that you’d like to call, into these triggers
-
Use something like the following script to make it work
void Update () {
if (_isMove == true && (transform.position.x) <= 4.5f) { transform.Translate (Vector3.left * 0.01f); transform.Translate (Vector3.left * 0.01f); } } public void OnPointerDown() { _isMove = true; } public void OnPointerUp() { _isMove = false; }
How do I fix the error on _isMove? @Saad_Khan
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonHandler : MonoBehaviour
{
void Update()
{
if (_isMove == true && (transform.position.x) <= 4.5f)
{
transform.Translate(Vector3.left * 0.01f);
transform.Translate(Vector3.left * 0.01f);
}
}
public void OnPointerDown()
{
_isMove = true;
}
public void OnPointerUp()
{
_isMove = false;
}
}