- Home /
Replace Input.GetAxis with Touch
Is there a way of converting this:
moveHorizontal = Input.GetAxis ("Horizontal");
moveVertical = Input.GetAxis ("Vertical");
transform.Translate (Vector3.right * moveHorizontal * speed * Time.deltaTime);
transform.Translate (Vector3.forward * moveVertical * speed * Time.deltaTime);
...so that it would work for touch?
I made buttons on my game where if I touch the up arrow key using my Android, it would go forward as if I pressed the up arrow key on my keyboard.
My touch code is currently this one, but it just detects the touch, it doesn't really move anything.
if (Input.touchCount > 0)
{
Touch theTouch = Input.GetTouch (0);
Ray ray = Camera.main.ScreenPointToRay(theTouch.position);
if(Physics.Raycast(ray, out hit) && hit.transform.tag == "UpButtonTag")
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Stationary || theTouch.phase == TouchPhase.Moved)
{
Debug.Log ("The Up Button is pressed!");
}
}
}
}
I have many more of these for the Left Button, Right Button, Down Button, etc, but I still don't know how to make it move my player like from the code above.
Thanks! I know someone already asked this question here, but its answers didn't work for me, and the asker didn't also chose a correct answer.
Answer by fafase · May 26, 2014 at 09:08 AM
public sealed class InputSystem:MonoBehaviour
{
public Rect[] axisVert;
public Rect[] axisHor;
private Rect[]r_axisVert;
private Rect[]r_axisHor;
public bool snap = true;
private static bool b_snap;
private static float xMov = 0;
private static float yMov = 0;
private static float ratio = 0.5f;
void Start(){
b_snap = snap;
r_axisVert = axisVert;
r_axisHor = axisHor;
}
public static float GetAxis(Axis axis)
{
Touch[] myTouches = Input.touches;
for(int i = 0; i < myTouches.Length; i++){
if(axis == Axis.Vert){
for(int j = 0; j < r_axisVert.Length; j++){
if(r_axisVert[j].Contains(myTouches[i].position)){
float value = 1 * 2 - 1;
if(b_snap)
return value;
else{
yMov = Mathf.MoveTowards(yMov, value, ratio * Time.deltaTime);
return yMov;
}
}
}
yMov = Mathf.MoveTowards(yMov, 0, ratio * Time.deltaTime);
return yMov;
}
// Same for horizontal axis
}
}
public enum Axis{
Hor, Vert
}
This will return -1, 0 or 1. If you need the smooth system Unity provide you would need to add a variable to store the previous value and lerp to the -1 , 1 or 0 when you release.
You need to add the component and provide the rect values in the inspector. The first rect is for the left/down rect, the second is for right/up. This could be modified to use a different approach like marking some objects as axis buttons and finding them on start but for now this is simple.
You use it with :
float hor = InputSystem.GetAxis(Axis.Hor);
Ok hold on a second I am trying it now, I have forgotten a couple of details.
Ok, I reviewed, now it should also include the lerping. You define it with snap. You can make it faster changing the value of ratio.
It works for me with normal input, but I owuld guess that should do it the same with GetTouch
Ok, I fixed this. Static method can only use static members so the rects needed to be turned into static ones. $$anonymous$$aybe this is not the best way in the end and you should maybe turn it all into a non-static method. Up to you.
Answer by Andres-Fernandez · May 26, 2014 at 09:11 AM
You can get the Rect from the button and check if it contains the position of the touch. If it does, set the logic for the movement (some booleans or whatever) instead of the Debug.Log.
Also, I've read that GUITextures get the OnMouseDown event.
Thanks, how do I make it move like the Input System Unity provides? Where it goes smoothly when I release the button. transform.translate doesn't do that, and I'm not sure how to use Lerp in this situation.
That's another question. The part that makes it more smooth is the Time.deltaTime. But if you look for a way to do it cooler you should check for SmoothDamp for the smooth translation, and google for movement inertia for the release (there are tons of scripts)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Move horizontally on touch 0 Answers
getPressure for touch pressure on Android or iOS 3 Answers
Touch joystick tutorials ? 0 Answers