So i’m creating an android game in Unity version 2020.3.13f1.
I have a script so the player moves upwards immediately on start and then the player should move around and dodge obstacles and so on…
But i have a problem, i only want the player to be able to move on the X axis. The Y axis should be “locked” so he can’t move up and down by touching on the screen.
I can’t figure out how to code this correctly, it always ends up in a mess somehow…
Here is my move by touch code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveByTouch : MonoBehaviour {
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0f;
transform.position = touchPosition;
}
}
}