This is my first time creating an Android Phone game with Unity. I want the user to be able to drag the space ship across the phone screen (x axis) but not up or down (y axis) by dragging it left or right with their finger. I also want to implement borders so the spaceship stops at the edge of the phone screen. How would I do this? I am using a C# script.
You modify only one axis.
void Update () {
if (Input.touchCount > 0)
{
this.transform.position = new Vector2(Input.GetTouch(0).position.x, this.transform.position.y);
}
}
With this code, you detect when the player touch the screen, and you move the object to the x position of the finger but you keep the same y possition
@WhiteCry How would I be able to do this with a mouse clicking to drag the spaceship left/right instead of a finger? Thank you so much for your help I really appreciate it.