- Home /
Question by
reeteshsuggula · Dec 27, 2019 at 03:58 AM ·
2d gameandroid buildtouch controls
can i get the code for android touch controls to move the object slide along y direction in 2D
Iam new to android touch controlling programs so could anyone let me get the code for it code i need is like a slider moving up and down like brick breaker
Comment
Answer by cvevea3 · Dec 27, 2019 at 04:01 AM
Here's left and right, it should be pretty easy to do from here. This code is already all over the internet. Not to be rude but you will be able to find the exact code with minimal searching
private void Move()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
// get the touch position from the screen touch to world point
Vector3 touchedPosx = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, Screen.height / 16, 10));
// lerp and set the position of the current object to that of the touch, but smoothly over time.
transform.position = Vector3.Lerp(transform.position, touchedPosx, Time.deltaTime * moveSpeed);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612220655im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Touchscreen Movement on buttons 1 Answer
Unity 2D: query for "mask/layer" bit on screen space without collider 1 Answer
StackOverflow error 1 Answer
Pressing multiple buttons in a single swipe [MOVEMENT] 2 Answers
How do I change this 2D script to touch controls to go up and down with swipe or on screen buttons? 1 Answer