# Dragging object on 2D plane from perpendicular view

 0 Dear Internet,I have an object on a 2D plane which I wish to drag along this plane. The plane is not infinite when it comes to placing the object. At first I performed a simple Raycast of the input to determine the hit position on the plane, and then moved the object to this position. The problem with this approach is that I am often having to view the 2D plane perpendicular, such that the plane is completely flat and so is not seen by the user. For example the plane is on the z-x axis and the camera is also on the z-x axis, looking along the z axis. In this case the ray either misses the plane, or intersects the plane at a very far distance, causing the dragged object to move to an odd position when I cast it to the nearest valid point.I tried creating a two step process, where the ray is cast onto another plane, facing the camera, centred at the object. Then from this hit position cast on to the original plane. If the camera is perpendicular this works. An example of this working is: The camera is facing the plane perpendicular, the input is below the plane. The original ray hits the red plane facing the camera and then the second ray hits the dragged destination point on the yellow plane.The problem is that this two step process then causes weird behaviour when the camera is not perpendicular, as the first ray can hit the camera-facing plane in the distance. Here is an example of this not working: The object is in the distance, thus the red plane facing the camera is in the distance. The original ray hits this and then, when projected again onto the final plane, the dragged location is completely incorrect.Essentially I need a way to drag an object around a non-infinite plane from any camera angle, including perpendicular. Either the process needs to somehow blend between the two options, or a different approach is needed. Any suggestions / advice is much appreciated :D more ▼ asked Jun 17 '11 at 05:28 AM TaintedLemon 124 ● 3 ● 3 ● 8 SisterKy 2.4k ● 33 ● 41 ● 59 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 A simple way to get around the perspective change would be to use straight mouse input instead of raycasts. So something like: ``````var horizontalSpeed : float = 2.0; var verticalSpeed : float = 2.0; function Update () { if (Input.GetMouseButton(0)){ // Get the mouse delta. This is not in the range -1...1 transform.position.x += horizontalSpeed * Input.GetAxis ("Mouse X"); transform.position.y += verticalSpeed * Input.GetAxis ("Mouse Y"); } } ``````(it's just a modified mouse-look script from the scripting guide) and just add an if-statement to constrain it to your plane's extents. more ▼ answered Jun 17 '11 at 05:56 PM Chris D 2.5k ● 5 ● 7 ● 25 The problem with this approach is it doesn't take into account the direction of the camera. And even if it did, the object wouldn't line up under the input position - so would be somewhat unintuitive. Jun 17 '11 at 10:16 PM TaintedLemon Yup, but I thought maybe you could use a different approach. How free is the camera to move around your scene? Jun 17 '11 at 10:22 PM Chris D Completely. They can view the scene plane from any angle with any orientation. Jun 17 '11 at 10:25 PM TaintedLemon add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x286
x251
x25
x6

asked: Jun 17 '11 at 05:28 AM

Seen: 1285 times

Last Updated: Jun 17 '11 at 10:25 PM