Common question. Please search and dont ask for scripts.
Need some help with Touchscreen Input.
I am new to Unity and I'm making a Infinite Runner that runs along the z axis. I have procedural generation, score system, and a working movement system that uses the Horizontal Axis to move left and right. Does anyone know how I could change the 'a' and 'd' inputs to touchscreen where by pressing the left side of the screen, the character would move left, and by pressing the right side, the character would move right? Thanks for helping, but I'm only interested in C# scripts
This is a common question and is easily searchable.
Please search before posting.
Answer by malta32 · Apr 01, 2016 at 04:04 AM
could you try detecting which side of the screen you push? Something like....
using UnityEngine;
using System.Collections;
public class Split : MonoBehaviour {
float width;
// Use this for initialization
void Start ()
{
width = Screen.width / 2;
}
// Update is called once per frame
void Update ()
{
if (Input.touches.Length > 0)
{
if (Input.touches[0].position.x > width)
{
Debug.Log("Right");
}
else
{
Debug.Log("Left");
}
}
}
}
Thanks! I just need to wire up the Debug Log to movement now right?
Yes, just replace the right and left debug messages with what you want to happen when the right and left sides of the screen are pushed.
if(.....x > width) { myUnit.$$anonymous$$oveOrder($$anonymous$$oveDirection.Right); }
Or however you are letting the unit know which way to move.
Thanks so much! That really helped. I really appreciate your answer.