[HELP] Swipe to move object

I decided to make a mobile app with a classmate for our school project. This is our first time making something in Unity so I figured I would eventually need help from this forum.

The idea of our app is to move a circle on a touchscreen with your finger to avoid squares that fly into the screen from all sides. You have to survive as long as possible.

The first thing we decided to program was the moving of the circle. I searched online and found some code, we then adjusted it to hopefully make it work in the latest version of C#. But the problem, is that it doesn’t work in the slightest and we have no clue what we’re doing wrong.

We would greatly appreciate it if someone could point out and improve the issues in the following code :wink:

using UnityEngine;
using System.Collections;

public class SwipeMove : MonoBehaviour
{

    void Start()
    {
        Debug.Log("start");
    }

    void Update() {

        if (Input.touches.Length > 0)
        {
            //record the touch location
            Touch t = Input.GetTouch(0);

            //touched
            Debug.Log("Screen has been touched");

            if (t.phase == TouchPhase.Began)
            {
                var
                                //save began touch 2d point
                                firstPressPos = new Vector2(t.position.x, t.position.y);

                //firstPressPos to debug
                Debug.Log(firstPressPos);

                if (t.phase == TouchPhase.Moved)
                {
                    var
                                 //save ended touch 2d point
                                 secondPressPos = new Vector2(t.position.x, t.position.y);

                    //print secondPressPos to debug
                    Debug.Log(secondPressPos);

                    var
                                    //create vector from the two points
                                    currentSwipe = secondPressPos - firstPressPos;

                    //print currentSwipe to debug
                    Debug.Log(currentSwipe);

                    //move Circle according to swipe
                    transform.position = currentSwipe;
                }
            }
        }
    }
}

Welcome!

It looks like you have the TouchPhase.Moved conditional nested inside the TouchPhase.Began one. Since t.phase can only be one of these, the TouchPhase.Moved logic will never be executed this way. Try it like this:

if (t.phase == TouchPhase.Began)
{
    //TouchPhase.Began logic
}
else if (t.phase == TouchPhase.Moved)
{
    //TouchPhase.Moved logic
}

Also, Transform.position is an absolute position in world space, but you’re setting it equal to the relative value currentSwipe. My guess is that you want to move the position of the object by the amount that the touch point has moved. This means that you’ll be adding some value to its position, not setting it to a new value.

However, Touch.position returns the touch coordinates in screen space (pixels on the screen), not in world space (units within the game world, which is what Transform.position uses). You can use Camera.ScreenToWorldPoint() to convert it to world space:

if (t.phase == TouchPhase.Moved)
{
    var deltaWorldPos : Vector3 = Camera.main.ScreenToWorldPoint(t.deltaPosition);    //Touch.deltaPosition is a shortcut for (newPos - oldPos), which eliminates the need for the TouchPhase.Began part of your code as it is for the purposes of movement
}

Note that just converting t.deltaPosition gives us a translation amount on the camera’s view plane. Now you can just add it to the position of the player object:

transform.position += deltaWorldPos;

Let us know if you’re able to get it working.

1 Like

First of all, thank you very much for your response, I really appreciate it.

So, I split the Touch.Began and Touch.Moved into separate conditions and they seem to execute now.

Then I put in the variable deltaWorldPos (what you typed didn’t seem to work, maybe because it’s a different version so I changed it)

else if (t.phase == TouchPhase.Moved)
                {
                    var deltaWorldPos = new Vector2(t.deltaPosition.x, t.deltaPosition.y);

                    transform.position += deltaWorldPos;
                }

As you can see I put the transform.position under that. the “+=” gave the following error:

Operator ‘+=’ is ambiguous on operands of type ‘Vector3’ and ‘Vector2’

So apparently I need to use something else to add the Vector to the current position of my game object. But what though?

UPDATE:
I have figured out a different way of doing it. Basically I get the position of the gameobject and add the deltaposition to that. But when I tested it at my phone, the circle did go the direction I swiped but it didn’t stop, it went off screen with just one little swipe. But I want the circle to stay under my finger. How do I fix this?

else if (t.phase == TouchPhase.Moved)
                {
                    var deltaSwipe = new Vector2(t.deltaPosition.x, t.deltaPosition.y);

                    var oldCirclePos = new Vector2(transform.position.x,transform.position.y);

                    var newCirclePos = oldCirclePos + deltaSwipe;

                    transform.position = newCirclePos;
                }

i had the best code for touch swipe on screen anywhere. I used this code in the “Rise Up” game to handle movement. I hope it will be helpful for you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandleMovement : MonoBehaviour
{
public static HandleMovement instance;
private Vector3 screenPoint;
private Vector2 mousepos;
public float hspeed;
private Rigidbody2D rb;

private Vector2 OffsetClicked;
private Vector2 OffsetReleased;
public Transform player;
private void Start()
{
rb = GetComponent();
instance = this;
}
public void Update()
{
if (Input.GetMouseButton(0))
{
Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
rb.MovePosition(curPosition);
OffsetReleased = curPosition - (Vector2)Camera.main.transform.position;
}
else
{
Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
rb.MovePosition(curPosition);
OffsetClicked = curPosition - mousepos;
}
}
}