Simpson G.
Simpson G.

Reputation: 111

Object Follow finger Only when it swipe on the Screen

I have a sphere in that is moving foward and I need it to follow my finger smoothly when it swipes on the screen but only in the x axis. I trying to make the exact movent like Rolling Sky, Color Road

enter image description here

The ball Should follow my finger when it swipes in the x axis and move smoothly. Just like in the games I mention before. I try OnMouseDrag and a lot of ways but it dont work correctly beacuse it dont dont follow the finger or it dont move while finger is swiping.

Upvotes: 0

Views: 805

Answers (3)

Simpson G.
Simpson G.

Reputation: 111

I solved it doing this and setting the variable NavigationMultiplier to 10000

Upvotes: 0

Horothenic
Horothenic

Reputation: 678

I think you should handle the input separately from the sphere.

Put this on a script to detect the swiping on the screen.

    bool inputBegan = false;
    Vector2 beganPosition;

    void Update()
    {
        if (!inputBegan && Input.GetMouseButtonDown (0))
        {
            inputBegan = true;
            beganPosition = Input.mousePosition;
        }
        else if (inputBegan && Input.GetMouseButtonUp (0))
        {
            //If you want a threshold you need to change this comparison
            if (beganPosition.x > Input.mousePosition) 
            {
                MoveBallLeft ();
            }
            else
            {
                MoveBallRight ();
            }
            inputBegan = false;
        }
    }

The movement of the balls you can achieve it adding or substracting Vector2.right * movingDistance to the sphere Transform if you want it to teleport or making the sphere move slowly using a targetPosition and currentPosition and each Update cycle add a little bit of transform distance.

Upvotes: 0

Julxzs
Julxzs

Reputation: 737

From what I understand, it seems you want to get the position of the user's finger and move the ball accordingly?

You could achieve this with Touch.position

Example:

private void Update()
{
    Touch touch = Input.GetTouch(0); // Get the touch data for the first finger
    Vector2 position = touch.position; // Get the position in screen-space
    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position); // Convert the position to world space

    Vector3 ballPosition = ball.transform.position;
    ball.transform.position = new Vector3(worldPosition.x, ballPosition.y, ballPosition.z); // Move the ball
}

Upvotes: 1

Related Questions