Move object by touch input

Hi

I’m trying to implement a touch function into my game which lets the player object move to the left or to the right (2D game) depending on the x position the user touches on the screen.

So for example, if the user presses for a long time on the left side of the screen (-x coordinate) the player object moves to the left.

I have the following code in C# but it doesn’t respond to my touches:

using UnityEngine;
using System.Collections;

public class Touch : MonoBehaviour {

	public GameObject player;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
		{
			Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

			//Check if it is left or right?
			if(touchDeltaPosition.x < 0.0f){
				player.transform.Translate(Vector3.left * 10 * Time.deltaTime);
			} else if (touchDeltaPosition.x > 0.0f) {
				player.transform.Translate(Vector3.right * 10 * Time.deltaTime);
			}

		}
	}
}

Please note that I’m new to Unity.

Thanks for your help in advance!

Delta position is the change in position since the last frame. You want the position of the touch instead.

Your if left/right statements will need to be changed as well since touchPosition will now be in pixels instead of world position.

Replace:

Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

With:

Vector3 touchPosition = Input.GetTouch(0).position;

Just in case someone is wondering how I managed to fix this. I used the following code.
This code is not optimized but it shows you how to get started:

using UnityEngine;
using System.Collections;

public class Touch : MonoBehaviour {

	public GameObject player;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
		{
			Vector2 touchPosition = Input.GetTouch(0).position;
			double halfScreen = Screen.width / 2.0;

			//Check if it is left or right?
			if(touchPosition.x < halfScreen){
				player.transform.Translate(Vector3.left * 5 * Time.deltaTime);
			} else if (touchPosition.x > halfScreen) {
				player.transform.Translate(Vector3.right * 5 * Time.deltaTime);
			}

		}

	}
}

How would you clamp this so that it cannot go past a certain area or range on the x axis? @monserboy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectTouchMove : MonoBehaviour {

private Vector3 _touchPosition;
private Rigidbody2D _rb;
private Vector3 _direction;
private float _moveSpeed = 10f;

// Use this for initialization
void Start () {
    _rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        _touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
        _touchPosition.z = 0;
        _direction = (_touchPosition - transform.position);
        _rb.velocity = new Vector2(_direction.x, _direction.y) * _moveSpeed;

        if (touch.phase == TouchPhase.Ended)
        {
            _rb.velocity = Vector2.zero;
        }
    }
}

}