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):