Touch drag gameObject with any camera angle

I know there are many posts/questions on touch drag, but everything I found normally involved a camera angle that was a multiple of 90 degrees.
I have a game for mobile devices that has a 60 degree camera angle, and we want users to be able to drag objects. The only issue is that dragging up and down on the screen affects both the Y and Z position of the gameObject to where its slightly off the finger, but follows it for the most part. I also don’t want to move the Y position of the gameObject

my basic function is:

while(touched)
{
   thisTransform.position = mainCamera.ScreenToWorldPoint(Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, (thisTransform.position-mainCamera.transform.position).magnitude));
   thisTransform.position.y = 0;
}

I’ve also tried using an offset version:

var offset : Vector3 = thisTransform.position - mainCamera.ScreenToWorldPoint(Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, (thisTransform.position-mainCamera.transform.position).magnitude));
while(touched)
{
   thisTransform.position = mainCamera.ScreenToWorldPoint(Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, (thisTransform.position-mainCamera.transform.position).magnitude)) + offset;
   thisTransform.position.y = 0;
}

The first code block works perfectly fine when using say a 90 degree camera, but that 60 degree angle throws it slightly off (and no I can’t change the camera angle). Any suggestions?

1 Answer

1

One idea. Construct a plane parallel to your surface. You can use a real plane or you can use a mathematical plane using Unity’s Plane class. When a use begins to drag the object, offset the plane upward by 1/2 of the height of the object (i.e. if it were a cube 1 unit in size, the planes would be moved upward 1/2 unit. To place the object, do a raycast against the plane and use the hit point. For a real plane, you can use Physics.Raycast(). For a mathematical plane, you can use Plane.Raycast(). Position the object at the hit point. That point should also be where position.y = 0;

i'll try to figure out how to implement that. is this example similar to what you are talking about? except instead of creating a cube and moving it, you just move the selected gameobject? http://forum.unity3d.com/threads/15802-Plane-Raycast-Example if not, do you think you could help with an example? i'll follow up if i figure it out in the meanwhile

pretty sure I got it working using that example and adjusting for my animations. thanks for the lead!

The example looks fine. I too would have use the mathematical plane.