So a couple weeks ago I asked a similar question here : http://answers.unity3d.com/questions/10268/have-a-gameobject-follow-the-mouse-in-sync-so-that-the-mouse-pointer-is-always-o
Today, I realized the solution to the problem wasn't so straightforward.
What I want is to be able to drag a cube around with the mouse, in a way that syncs perfectly with the mouse. The mouse cursor should always remain over the object, and visually, the object should move as much as the mouse did. This should work no matter what the rotation angle of the camera is. The cube should also be exactly under the point where we clicked, it shouldn't snap to its center.
Here is what I have that works, when the camera is looking perfectly perpendicular to the Z axis, but breaks when the camera is rotated or moved around at different angle:
This code works, if the camera is pointing straight at the cube from in front. If you rotate the camera, say 45 degrees, it will stop working.
I want a solution that does not use Ray-casting.
asked Jul 07 '10 at 05:23 PM
I think your question could use come clarification as to how you are moving your camera, but I think I get your meaning.
Have you considered something like:
This way the object is not centred to the mouse, but will keep its relative position on screen to where the mouse was clicked.
If the camera rotates in this script, then you can easily check if the mouse is held down and call transform.position = Camera.mainCamera.ScreenToWorldPoint(screenPosition); when the camera rotates
If the camera rotates in a separate script (which it likely does), I suggest making the relevant data public to the camera rotation so that it can update the object's transform if applicable (the mouse is still down). The exposed data could be the selected object and its stored screen position or a public function like updatePosition which will make the call to set the transform and then your rotate function and mousedrag can both call this.
answered Jul 07 '10 at 10:58 PM
Try using something like
Where distanceFromScreen is a fixed distance from the screen.
You'll have to use raycasting if you want to make sure your object doesn't go "behind" something, though. So you'd cast a ray, do a trace, and put your object's position at either what I had before, or the hit.point, whichever is closer.
answered Jul 07 '10 at 05:56 PM