- Home /
Dragging a Camera along a plane / Where's the error?
I'm trying to drag the camera with the mouse (respectively a finger-touch) along a plane. But for some reason the camera doesn't move in the same speed as the touch is moving relative to the scene. It moves to slow. I have no idea what is wrong in my code:
float distance;
//Getting the ray pointing towards the point in the scene, relative to the screen-pos
//The camera looks straight down to the scene (a floorplan)
Ray rayNow = m_FloorPlanCam.ScreenPointToRay (finger.position);
//Getting the distance from ray origin to a plane (which is Plane(vector.up, vector.zero))
m_FloorPlane.Raycast (rayNow, out distance);
//Now getting the actual point in the scene
Vector3 planePointNow = rayNow.GetPoint (distance);
//The same as above for the previous finger position
Ray rayBefore = m_FloorPlanCam.ScreenPointToRay (finger.position - finger.deltaPosition);
m_FloorPlane.Raycast (rayBefore, out distance);
Vector3 planePointBefore = rayBefore.GetPoint (distance);
//The differens between this two points in the scene give me the amount to move the camera
Vector3 sceneMove = planePointBefore - planePointNow;
m_CurrentCam.transform.position = m_CurrentCam.transform.position + sceneMove;
Thank you for your assistance.
I don't spot an obvious problem. It would be helpful to have the full script. But suggest you approach your logic a bit differently. On the mouse/finger down, calculate the offset between the camera and the point hit on the plane. On the mouse/finger move position the camera at the hit point plus the offset.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171645im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Alternative to ScreenPointToRay needed 1 Answer
Need help understanding ScreenPointToRay 2 Answers
Plane.Raycast() out distance incorrect? 1 Answer
ScreenPointtoRay Problem, how does it work!?! 1 Answer
Raycast not detecting planes 4 Answers