- Home /
Drag And Drop with Perspective Camera
Hi!
When I have the mainCamera in Orthograpic view I have a working drag and drop "system" but when I change It to Perspective view the drag and drop code doesn't work like It used to, and I need the Perspective view on the Camera.
So my question is: Is It possible to have the camera on Perspective view and still have a working Drag and Drop "system" and how?
Code:
void OnMouseDrag ()
{
if (!assembledBL) { //Drag the hexKey with mouse
point = Camera.main.ScreenToWorldPoint (Input.mousePosition);
point.y = transform.position.y;
transform.position = point;
} else if (assembledBL) { // twist the hexKey
xScroll = Input.GetAxis ("Mouse ScrollWheel");
this.transform.Rotate (xScroll * 10f, 0f, 0f);
}
}
Answer by benni05 · Mar 12, 2014 at 11:36 AM
You have to provide the z position when calling ScreenToWorld point:
point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane))
Now you'll have world position that is at your clipping plane of the camera (i.e. on the display so to speak). In a coordinate system where x and y define the screen space and z is the depth you would then change the x and y position of your transform based on this world position (point) but keep its z position like so:
transform.position = new Vector3(point.x, point.y, transform.position.z);
If your coordinate system is different then change this accordingly.
Thanks!
But I have a slight problem, I want to change it at it's X and Z position, but when I click the object it changes it's Y position to the same as the camera..
Your code does keep the y position because your point Vector looks like this:
(world position of mouse x position, transform.position.y, 0)
But you are implicitely using a z position of 0. $$anonymous$$aybe that is your problem? Just debug it... and print those coordinates before and after clicking.
I'm kinda stuck, I'm able to get other positions. But im not able to "Drag" it..
Sorry, should be
Vector3.Distance(transform.position, Camera.main.transform.position)
of course. $$anonymous$$ethod expects Vector3's as arguments. Since your Camera is obviously looking down the Y axis we deter$$anonymous$$e the distance between the Camera and your object as the 3rd parameter for the ScreenToWorld function.
But I aint getting no more errors, the problem is fixed and the code works almost as I want it to :)
Thanks for your help
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Camera issue (storing temp variable C# problem) 3 Answers
Distribute terrain in zones 3 Answers