I’m creating a top down game with a floating camera. I’ve got everything working fine except for one thing. I’d like the screen rotation and zoom to be around the cursor/finger center instead of the center of the screen.
I’ve had this working in Marmalade by working with the view transform, but I can’t figure out how to do the equivalent in Unity.
I’d think that translating the point I touched to the center of the screen then rotating/zooming it then translating it back should achieve the effect I want, but I’m not able to figure out how to do that.
Can anyone help me with that?
I’ve added debug statements (not shown) and the parameters look like they are correct
// MouseXEuler is the rotation angle
//Distance is the distance (basically the zoom)
//TwistAngle is the amount that the camera should be rotated.
void UpdateCamera(float offsetX, float offsetZ) {
var rotation = Quaternion.Euler(MouseXEuler, MouseYEuler, 0);
var position = rotation * new Vector3(0.0f, 0.0f, -Distance) + Target.position;
transform.rotation = rotation;
transform.position = position;
//Translate screen position before rotation of camera doesn't work???
Vector3 p = transform.position;
p.x += offsetX;
p.z += offsetZ;
transform.position = p;
transform.Rotate(new Vector3(0, 0, -TwistAngle));
p = transform.position;
p.x -= offsetX;
p.z -= offsetZ;
transform.position = p;
}