Hi!
I might not use the proper search keywords, but I haven’t found any answer to this question: how to make an actual grab-and-drag camera – a camera that moves parallel to a plane (seen in perspective) in such a way that the point of the plane that was projected on cursor’s location when the player pressed the mouse button, remains under cursor’s location while the player holds the button and moves the mouse, like if the cursor was pinned onto the plane.
I’ve just read a recent post on the forums: it seems to ask a similar question but it’s got no proper answer.
My code does the following:
- retrieve the proper plane’s point from cursor’s position on screen (using Camera.ViewportPointToRay and line-plane intersection)
- get the delta vector between this plane’s point and the previous plane’s point retrieved (from cursor’s position on previous frame)
- translate the cam to the opposite of this delta vector.
I thought that way I would always get the right distance to move the camera (since it’s moving parallel to the plane), whatever the distance of the plane’s point from the camera… But I guess I was wrong since the cursor is still not “pinned” onto the plane (the result is not really better than on my “basic” version, when I translated the cam at an arbitrary constant speed and relative only to the cursor’s movement)
There might be an additional step to translate the cam, instead of just using the opposite of the delta vector; but I don’t know what operations/transformations, I am not good enough in solid geometry.
Yet my case is rather simple: the plane is normal to the z axis, thus the cam translates on x,y only.
Can anyone answer this little solid geometry problem?
EDIT - SOLUTION (principle – for code plz see the answer I posted below):
I’ve just come up with the solution and I think other people might be interested:
My mistake was on step 2, when getting the “previous position” that I will substract from the position of the plane’s point I retrieve from the cursor’s position on the current frame:
One should not use the plane’s point that was retrieved on the previous frame (corresponding to the cursor’s position on the previous frame).
Instead, because the cam moves every frame, one must retrieve – on the current frame thus with the current cam position – the plane’s point corresponding to the cursor’s position on the previous frame.