- Home /
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!
This article by The Game Contriver helped me solve this... $$anonymous$$ove Object To Tap/Click Position
Answer by Maui-M · Feb 14, 2014 at 06:36 PM
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;
Those are vector2, position is vector3. Something is wrong. Being in 2D still requires Vector3 for position.
Your right, updated the code. You can just use a Vector3, it will just have a z value which you wouldn't have any use for.
You don't need Vector3, but you do need to change from 'deltaPosition' to 'position' as indicated by @$$anonymous$$aui $$anonymous$$. Then you are going to have to compare against Screen.width / 2.0 rather than 0.0 to deter$$anonymous$$e left vs. right side of the screen. Screen coordinates start at (0,0) in the lower left of the screen.
Thank you very much guys, I've fixed it! The Unity3D community rocks :)
Answer by monserboy · Feb 16, 2014 at 06:59 PM
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);
}
}
}
}
Answer by malcolmmaima · Jan 24, 2017 at 03:40 PM
Been struggling with something similar for a while now, Your code works perfectly!!! Thank you!!
Answer by gararules112 · May 14, 2018 at 02:26 PM
How would you clamp this so that it cannot go past a certain area or range on the x axis? @monserboy
Answer by CrazyAli · Oct 07, 2019 at 06:35 PM
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;
}
}
}
}