Mouse drag code works with Orthographic camera not with Perspective camera

I have some code I wrote while my camera was set to orthographic but I now realize a perspective camera is going to be better for what I’m trying to accomplish. The problem I’m having is my game worked fine in ortho but when its in perspective mode my object click/drag code doesn’t work anymore.

The only thing this code does when in perspective is when I click the object it moves to the center of the screen and snaps back to it’s old position because I have that in code someplace else to move it back.

void OnMouseDrag()
{
    // Get our current mouse position
    currentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    currentPosition.z = transform.position.z;

    transform.position = currentPosition;

    Screen.showCursor = false;
}

Can I not do ScreenToWorldPoint with a perspective camera or something?

There is something mysterious about how ScreenToWorldPoint uses the z value.

Try this:

function Update () 
{
	transform.position = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
}

but manipulate the constant 10 as you need to.

I’ll mess with it when I get home but isn’t what I’m doing essentially the same thing? I just don’t understand how the code can work when I set the camera to orthographic but not a perspective camera… its odd… I don’t have the script attached to the camera or anything so I’m not sure what its trying to do…

I didn’t look at your example carefully, but in general, picking behavior can certainly differ between orthographic and perspective transforms. Depending on how you’re handling the distance factor, the fact that a picking ray for a perspective projection generally passes through the camera position while the picking ray for an orthographic projection generally does not could certainly cause the behavior to be different.

Anyone got an idea how to get this working with a perspective camera?