Question by
archie94 · Dec 24, 2015 at 01:21 AM ·
unity 5screentoworldpointdragging
How to drag objects by mouse when the camera is vertically above the terrain and facing it ?
I have a terrain , on it are some objects instantiated at run time. I have a camera above the terrain and facing the terrain , orthographic projection , so I have a birds eye view of the terrain. Now I am unable to drag these objects with mouse. I have written the following code
float yPosition;// stores the transform.position.y of gameobject
GameObject cam2D;
void OnMouseDrag()
{
Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
Vector3 objectPosition = cam2D.GetComponent<Camera> ().ScreenToWorldPoint (mousePosition);
transform.position = new Vector3 (objectPosition.x, yPosition, objectPosition.z);
}
This code is attached to every objects I instantiate. I am able to drag objects if I have a FPSController on the terrain ( with slight modification of the given code) however I am totally lost when I try to do so in birds eye view. I think I am missing something in the ScreenToWorldPoint part maybe ? Thanks
Comment