- Home /
Drag object with touch on y axis
First off all my game is 2D. I want to drag my object up and down to dodge stuff. I want my object to move up and down based on a touch. The game "The Line Zen" is a good example of this, the only difference is that you move the object on the x axis in this game. To summarize: when you touch the screen and drag your finger up or down, the object follows. How can I do this? Appreciate answers.
With a mouse I would do something like this: In update-function: if(Input.Get$$anonymous$$ouseButton(0)){ //checks if mouse is pressed gameObject.transform.position = Vector3.$$anonymous$$oveTowards(gameObject.transform.position, Camera.main.ScreenToWorld(Input.mousePosition), Time.deltaTime); //move the gameobject towards the mouse-position (translated into world-position) }
Never worked with touch before, but should be the same principle. Look at http://docs.unity3d.com/ScriptReference/Touch.html
EDIT: To limit it to the Y-axis, just save the mouse-position as a separate vector3, make a new vector3(gameObject.transform.position.x, mousePos.y, gameObject.transform.position.z) and move the gameobject towards this vector.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Rotating a specific axis towards a object 3 Answers
Multiple Cars not working 1 Answer
How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers
Triggers Interacting with Triggers 0 Answers