Moving The 3d Ball In Unity (left And Right) With Touch.

I’m making a game in unity where you need to avoid obstacles.
I would like to have drag movement, when you put your finger ON the ball it instantly follows your finger, without delay. Also, it shouldn’t teleport to the position of my finger or follow it if I touch somewhere else on the screen.
Another problem is that the ball should move smoothly and in only two directions, left and right.
If someone has an idea how to do it I would be grateful.

Note: I already have a code for touch (drag) movement that I found on youtube, but it’s a little buggy. Ball is not moving smoothly, and it can move in every direction.
```csharp
** using UnityEngine;
using System.Collections;

public class TouchMove : MonoBehaviour
{
    public float speed = 0.02f;

    void Update()
    {
        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
            {
                Plane plane = new Plane(Vector2.up, transform.position);
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                float dist;
                if (plane.Raycast(ray, out dist))
                {
                    transform.position = Vector3.MoveTowards(transform.position, ray.GetPoint(dist), Time.deltaTime * speed);
                }
            }
        }
    }
}**

```

You can raycast a tap on the screen into the scene and see if you hit a collider on the ball to decide if you touched it. If the ray doesn’t hit it when your finger goes down, then don’t move it until you lift finger and re-tap. This implies some type of boolean state to track “am I in a movable state now?”

If your camera is facing +Z (default) then +X will be left to right. In line 20 you are assigning the entire position, which is x,y and z.

Instead create a new Vector3 object that is a copy of the current transform.position, and ONLY assign the new x position before you put that Vector3 back into the transform.position.

As for smoothness this implies some type of low pass filtering. Instead of assigning a new position to the object you can blend it with the previous position so that it may take a few frames to get to where it goes, but it will jitter less. You can blend it with a lerp or a ratio or even some kind of tweening or easing.

1 Like

Your requirements of instantly following your finger, but no teleport using smooth movement, are contradictory requirements (instantly moving without delay means teleport). Generally you achieve smooth movement through some form of movement interpolation, meaning you move it over several frames to the intended destination instead of instantly moving to where it will be going. So you can choose either instant movement or smooth movement, but not both. The faster you move the less smooth the movement may appear.

As for limiting where the object can be moved, you can just apply the position change to a single axis of the object instead of setting all axis like you’re doing on line 20.