This code works for the X and Z axis, but it totally screws up at the Y axis. This script is attached to my camera object and its looking down in orthographic mode with a Y value of 15. However when I click, it will reset the Y value to 0 causing my camera to go under the “floor”. I have tried to do this myself and have also tried other scripts on forums but I keep getting this Y problem.
void Drag()
{
Vector3 dragOrigin = Vector3.zero;
if ( Input.GetMouseButtonDown(0)){
dragOrigin = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
dragOrigin = camera.ScreenToWorldPoint(dragOrigin);
}
if ( Input.GetMouseButton(0)){
Vector3 currentPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
currentPos = camera.ScreenToWorldPoint(currentPos);
Vector3 movePos = dragOrigin - currentPos;
transform.position = transform.position + movePos;
}
}
I just want to be able to drag the floor.