# How to move my cube left or right using touch Inputs (C#) in unity

I am trying to create a 3d game in unity. I have a cube in unity and I want it to move left or right on swipe . It should move to a distance till where user swipe or take their finger off of a screen. I want it to move fast or the cube should move fast on swipe and cube should flow smooth. Like in the gif there is a circle and the user use their finger to move the circle till wherever the user swipe it and it should be instant and I want my cube to move like that at a distance till where user swipe their finger and it should be instant and fast. I don't know how to do it. Any help is appreciated!

• answers.unity.com/questions/1223838/… Nov 3, 2017 at 15:11
• To clarify your other requirements: 1) the swipe has to start on the cube? Or if the cube is on 1 and I swipe 4->5, should the cube not move at all, or move the distance of the swipe 1->2, or move all the way to the end of the swipe 1->5? 2) Should the movement wait until after the swipe to take effect, or am I effectively dragging the cube, where it's moving simultaneously with the movement of my finger? 3) Does the swipe have to be fast and in a straight line to count as a swipe, or does a slow or curvy motion work too? Nov 3, 2017 at 16:21
• @DMGregory 1) Yes, the swipe has to start on the cube. Yeah if cube is on 1 and you swipe 4 -> 5 then it should not move at all. It should only move when the finger of the user is on the cube and they drag or swap the cube in any direction. If I swipe the cube left then it should move left. It should move the distance of whatever is swiped by user. 2 ) No, it should move instantly. Like when I swap the cube, it should move instantly, so yeah it should kinda drag. 3) No, it should be fast and in a straight line and not curve or slow. Nov 3, 2017 at 17:22
• Why not to move the cube under the finger then? Or I'm just missing something because I've broke my brain trying to figure out the desired behaviour... Nov 4, 2017 at 4:53
• I suggest you add all the information you added in the comments to the question. Nov 8, 2017 at 17:40

 if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero

if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
// get the touch position from the screen touch to world point
Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
// lerp and set the position of the current object to that of the touch, but smoothly over time.
transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime);
}
}


Basically, when this is attached to the object you want to move (this is for 2d objects, though it can be tweaked for 3d), it gets the touch location and moves itself to that spot. Hope this helps!

EDIT:

You can tweak for 3d by changing the value of your object's Z axis. Example:

 Camera.main.WorldToScreenPoint(touch.position.x, touch.position.y,transform.position.z)

• Can you tell me that for 3d? Nov 10, 2017 at 2:32
• @Hemlata the only difference for 3D is that you would want a better Z-position than the fixed "10" in the example code here. Replacing the 10 with Camera.main.WorldToScreenPoint(transform.position).z is probably a good first step. Nov 10, 2017 at 10:42

You can try the following script

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

public class SelectObject : MonoBehaviour {

// Use this for initialization
GameObject hitObj;
RaycastHit hit;
private float speed = 1;

void Start () {

}

// Update is called once per frame
void Update () {
foreach (Touch touch in Input.touches) {
switch (touch.phase) {
case TouchPhase.Began:
Ray ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray, out hit, 10)) {
hitObj = hit.collider.gameObject;
}
break;
case TouchPhase.Moved:

// If the finger is on the screen, move the object smoothly to the touch position
float step = speed * Time.deltaTime; // calculate distance to move
if(hitObj != null)
hitObj.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, hitObj.transform.position.z));
break;
}
}
}
}


I hope I can help in this matter,but for rotation:

public class TouchRotate : MonoBehaviour
{
public GameObject obj;
public float power;
float posTouchBegan;

private void Update()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
posTouchBegan = Input.GetTouch(0).position.x;
}

if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
if (Input.GetTouch(0).position.x > posTouchBegan)
{
obj.transform.Rotate(new Vector3(0, 1 * Time.deltaTime * power, 0));
}
if (Input.GetTouch(0).position.x < posTouchBegan)
{
obj.transform.Rotate(new Vector3(0, -1 * Time.deltaTime * power, 0));
}
Debug.Log("touch position:  " + Input.GetTouch(0).position.x);
}
if (Input.GetTouch(0).phase == TouchPhase.Stationary)
{
posTouchBegan = Input.GetTouch(0).position.x;
}
}
}
}