How to fit the this "drag by touch" script to my gameobject?

hi guys,
I’ve got a simple scene that includes a camera, a map that represented by Quad object and a gameobject that i added.
The gameobject is the player in the game and its include an animation of movment.
the result is that the player seems to walk on the map.
I found in the web some script that supposed to make my player moveable by touch dragging him on the smartphone screen.
The instructions for using the script was: “Create a new *.cs script with the following code. Place this code on the AR Camera to enable users to touch and drag an object. The target object must have a mesh collider”.
But when i put it on my camera and try to run it on my phone, its cause my map to move by dragging and not my player, even if i add a mesh collider to it like it written in the instructions.
Im new in unity and i will be very happy to get some help from you guys.
Thanks!

The script:

using UnityEngine;
using System.Collections;
public class DragObject : MonoBehaviour {
   public GUIText message = null;
   private Transform pickedObject = null;
   private Vector3 lastPlanePoint;
   // Use this for initialization
   void Start () {
   }
   // Update is called once per frame
   void Update () {
        Plane targetPlane = new Plane(transform.up, transform.position);
        //message.text = "";
        foreach (Touch touch in Input.touches) {
            //Gets the ray at position where the screen is touched
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            //Gets the position of ray along plane
            float dist = 0.0f;
            //Intersects ray with the plane. Sets dist to distance along the ray where intersects
            targetPlane.Raycast(ray, out dist);
            //Returns point dist along the ray.
            Vector3 planePoint = ray.GetPoint(dist);
            //Debug.Log("Point=" + planePoint);
            //True if finger touch began. If ray intersects collider, set pickedObject to transform
                of collider object
            if (touch.phase == TouchPhase.Began) {
                 //Struct used to get info back from a raycast
                 RaycastHit hit = new RaycastHit();
                 if (Physics.Raycast(ray, out hit, 1000)) { //True when Ray intersects colider.
                 If true, hit contains additional info about where collider was hit
                     pickedObject = hit.transform;
                     lastPlanePoint = planePoint;
                 } else {
                     pickedObject = null;
                 }
            //Move Object when finger moves after object selected.
            } else if (touch.phase == TouchPhase.Moved) {
                 if (pickedObject != null) {
                     pickedObject.position += planePoint - lastPlanePoint;
                     lastPlanePoint = planePoint;
                 }
            //Set pickedObject to null after touch ends.
            } else if (touch.phase == TouchPhase.Ended) {
                 pickedObject = null;
            }
        }
   }
}

got it from: https://developer.vuforia.com/library/articles/Solution/Unity-Drag-an-AR-object-on-screen-with-finger

Im also adding pictures of my objects properties before i tried adding the DragObject script

Camera (without the script):

Background:

Player (called ‘spider’, without mesh collider):

Hmm, the script looks correct to me.

It’s not hard to see why this would drag your map, if the touch hits the map — it’ll drag anything with a collider. You could avoid that by checking what object is hit (perhaps using tags) before you start dragging.

But if you’re sure you’re touching the player, but the player still doesn’t move, then something is wrong with the collider. Are you sure it’s big enough?

Also, are there going to be other things you can move besides the player? Or should a touch/drag anywhere always move the player? If that’s the case, then this script isn’t really appropriate.

Thanks for the quick answer.
i tried changing the box collider size and it didnt help, creating tags helped and now the map isnt moving but the is not moving too…
I still think the problem is with the player’s collider, just dont know what to change or what to check, should i change the type of the collider? or maybe some spasific property?..
Hope some one will figer it out :slight_smile:
Meanwhile, i will keep changing the collider and maybe i’ll get lucky :slight_smile:

I don’t see how you got anything to move.

I tried the script, and didn’t really see that code causing any noticeable movement.

It seems like planePoint is always the camera’s position.

I suspect this line pretty much moves it zero:

pickedObject.position += planePoint - lastPlanePoint;

There is a line in the original code like this:

//Debug.Log(“Point=” + planePoint);

You might want to look at planePoint and see if it is ever not the camera position. Maybe put it in a text object?

I thought about it some more, and I think your camera must be orthographic for this to work at all.

If your camera is Perspective, I suspect nothing will be moveable, as I originally encountered.

Also, the page here:

https://developer.vuforia.com/library/articles/Solution/Unity-Drag-an-AR-object-on-screen-with-finger

says “The target object must have a mesh collider.”

As far as I can tell, you don’t need a “Mesh Collider”, but you do need a collider.

Also, that page says “Place this code on the AR Camera”.

So, the script belongs on the camera only. It’s not supposed to be added to anything else as far as I can tell.

I have a scene with a Sphere and a Cube, and I can drag them around the Android tablet. I pretty much just made sure the camera was Orthographic.

Also, I believe Physics.Raycast is for 3d colliders, which explains why it didn’t catch your player (which had a Box Collider 2D). So, if you remove the Box Collider 2D and add a Box Collider, that is a step in the right direction.

1 Like