- Home /
How to replace input axis keyboard button with touch button?
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.
i know this is old... but Holy Baby Jesus with a Golden Diaper... Thank you!!!
Answer by Saad_Khan · May 31, 2015 at 12:14 PM
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:
http://docs.unity3d.com/ScriptReference/Input.GetTouch.html
Recommend solution
Although, I would recommend that if you are using the latest Unity UI .
1) Create a button , and add an event trigger component
2) 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.
3) Hook corresponding functions like "OnPointerDown" etc that you'd like to call, into these triggers
4) 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;
}
Thank you! Your answer gave me a good clue of understanding touch input principles.
Its just make the game object move in anti-clockwise way,....
Thank you so much! The clearest explanation i've found!
Please, you deserve a Grammy. You have given me life. Thank you so much!
Answer by unity_9d5-VgjizaCUeQ · Dec 28, 2017 at 10:15 AM
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;
}
}
using System; using UnityEngine; using UnityEngine.UI; using UnityEngine.Scene$$anonymous$$anagement;
public class ButtonHandler : $$anonymous$$onoBehaviour
{
bool _is$$anonymous$$ove; //You forgot to add a bool:)
void Update()
{
if (_is$$anonymous$$ove == true && (transform.position.x) <= 4.5f)
{
transform.Translate(Vector3.left * 0.01f);
transform.Translate(Vector3.left * 0.01f);
}
}
public void OnPointerDown()
{
_is$$anonymous$$ove = true;
}
public void OnPointerUp()
{
_is$$anonymous$$ove = false;
}
}