Pan a rotated Camera
I have made a simple script to pan a perspective Camera with mouse drag. The pan should be "pixel perfect", meaning that when I move the camera around, I always have the same portion of the screen under my mouse pointer.
The script works fine when the Camera has no rotation on Y axis. If I add rotation (45 degrees), the movement follows the rotated axis X and Z and it's weird.
This is my script:
public class CameraMovement : MonoBehaviour { private Vector3 prevPos;
private Vector3 touchOrigin;
private Vector3 currentTouchPosition;
private Vector3 basePos;
void Update()
{
if (Input.GetMouseButton(0))
{
if (touchOrigin == Vector3.zero)
{
touchOrigin = Input.mousePosition;
basePos = transform.position;
}
currentTouchPosition = Input.mousePosition;
var targetPosition = new Vector3(basePos.x + (touchOrigin.x - currentTouchPosition.x) * .01f,
transform.position.y, basePos.z + (touchOrigin.y - currentTouchPosition.y) * .01f);
transform.position = Vector3.Lerp(transform.position, targetPosition, 20f * Time.deltaTime);
// transform.Translate(targetPosition - transform.position, Space.World);
}
}
}
as you can see I also tried to use Translate with the Space.World parameter but the behaviour is exactly the same.
I also tried to attach the script to a parent object of the rotated camera and move that object in both World and Self space but it doesn't work either.
Is there a way to achieve this? What am I doing wrong?
Your answer
Follow this Question
Related Questions
Problem with pan and rotate camera with mouse 0 Answers
Camer do not render correctly! 0 Answers
How to make a FreeLook camera 1 Answer
How to make the camera NOT rotate with it's father 1 Answer
Multiple Monitor Support 0 Answers