0
\$\begingroup\$

enter image description here

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!

enter image description here

\$\endgroup\$
  • \$\begingroup\$ answers.unity.com/questions/1223838/… \$\endgroup\$ – Jimmy Nov 3 '17 at 15:11
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Nov 3 '17 at 16:21
  • \$\begingroup\$ @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. \$\endgroup\$ – Hemlata Nov 3 '17 at 17:22
  • \$\begingroup\$ 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... \$\endgroup\$ – Ocelot Nov 4 '17 at 4:53
  • 1
    \$\begingroup\$ I suggest you add all the information you added in the comments to the question. \$\endgroup\$ – Alexandre Vaillancourt Nov 8 '17 at 17:40
2
+50
\$\begingroup\$

Here is an example script to answer your question:

 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)
\$\endgroup\$
  • \$\begingroup\$ Can you tell me that for 3d? \$\endgroup\$ – Hemlata Nov 10 '17 at 2:32
  • \$\begingroup\$ @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. \$\endgroup\$ – Jimmy Nov 10 '17 at 10:42
0
\$\begingroup\$

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;
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.