- Home /
Drag object relative to camera
I'm using the Vuforia AR library and I'm trying to let the user drag the object across the screen with their finger. I've tried all sorts of different methods but everything I've found only drags the object along the axis defined by the object and not the camera.
I've tried using Camera.main.tranform.forward
which I think is the key, but I haven't been able to implement it properly. Can someone kindly point me in the right direction?
Edit: This is the code I ended up using thanks to robertbu
[SOLVED]
using UnityEngine;
using System.Collections;
public class DragObject : MonoBehaviour
{
private float maxPickingDistance = 10000;// increase if needed, depending on your scene size
private Vector3 startPos;
private Transform pickedObject = null;
// Update is called once per frame
void Update ()
{
foreach (Touch touch in Input.touches)
{
//Create horizontal plane
Plane horPlane = new Plane(Vector3.up, Vector3.zero);
//Gets the ray at position where the screen is touched
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (touch.phase == TouchPhase.Began)
{
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, maxPickingDistance))
{
pickedObject = hit.transform;
startPos = touch.position;
}
else
{
pickedObject = null;
}
}
else if (touch.phase == TouchPhase.Moved)
{
if (pickedObject != null)
{
float distance1 = 0f;
if (horPlane.Raycast(ray, out distance1))
{
pickedObject.transform.position = ray.GetPoint(distance1);
}
}
}
else if (touch.phase == TouchPhase.Ended)
{
pickedObject = null;
}
}
}
}
Hi there,
falantar14's code is exactly what i am looking for. The only thing i would like to change is, that the object is not snapping to the center when i touch it.
I just can't figure out how to fix the snapping to the center of the object when i start dragging. I found several Q/A in this forum where an offset is mentioned but when i try to implement any of those ideas in the code the debugger throws exceptions. Do you have any ideas how to solve my problem?
Thanks in advance!
Post a new question with your code. To solve this problem you need to save and use the offset between where the mouse went down and the center of the object you are dragging. For example, you see a v3Offset in the code in my answer.
how can i drag and snap an object by using touch and build in android application ? can i have some helpfull codes hehe :D
Answer by robertbu · Oct 30, 2013 at 03:57 PM
One solution is to use Unity's mathematical Plane class. You construct the plane using the Camera's forward and then use Plane.Raycast() to find the point on the plane. You should be able to borrow code from this answer (though you will need to make a few changes):
http://answers.unity3d.com/questions/486097/mouse-clicking-on-the-plane-and-drag-to-other-posi.html
Thanks, I will look into it and accept your answer if it works.
Here is some sample code that implements a drag and drop relative to the camera. Attach it to the object(s) you want to drag:
using UnityEngine;
using System.Collections;
public class Drag: $$anonymous$$onoBehaviour {
private float dist;
private Vector3 v3Offset;
private Plane plane;
void On$$anonymous$$ouseDown() {
plane.SetNormalAndPosition(Camera.main.transform.forward, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast (ray, out dist);
v3Offset = transform.position - ray.GetPoint (dist);
}
void On$$anonymous$$ouseDrag() {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast (ray, out dist);
Vector3 v3Pos = ray.GetPoint (dist);
transform.position = v3Pos + v3Offset;
}
}
Thanks I was actually able to tweak the code from your original answer to suit my needs. I'll post it here.
For anyone who needs this for 2 vectors (2D), it can be easily modified. As follows:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class Drag : $$anonymous$$onoBehaviour {
private Vector2 screenPoint;
private Vector2 offset;
void On$$anonymous$$ouseDown() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
}
void On$$anonymous$$ouseDrag()
{
Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
transform.position = curPosition;
}
}
Your answer
Follow this Question
Related Questions
Box Drag and throw away script for mobile? 0 Answers
unity2D touch drag 0 Answers
Dragging Camera based on Touch 0 Answers