- Home /
Dragging object on 2D plane from perpendicular view
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
Answer by Chris D · Jun 17, 2011 at 05:56 PM
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.
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.
Yup, but I thought maybe you could use a different approach. How free is the camera to move around your scene?
Completely. They can view the scene plane from any angle with any orientation.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Intersection point of mouse with ZX plane 1 Answer
can I drag out the scale of a plane? 1 Answer
Help to fix my drag code [SOLVED] 1 Answer
hot to use raycast 2 Answers
Click and drag a plane 1 Answer