- Home /
How can I drag a GameObject along a fixed line?
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.
Unity Answers is not a free coding service.
Please, read carefully the FAQ
Answer by RadonRaph · May 02, 2018 at 10:13 AM
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
Answer by stephenshettler02 · May 03, 2018 at 06:24 AM
@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.