思路整理:
缩放前:
缩放后:
记录缩放前鼠标的屏幕坐标 A,计算鼠标位置对应的世界坐标 A_world
缩放完成后,根据当前屏幕下A所对应的世界坐标A1_world
计算A1_world 和 A_world 的偏移量
移动摄像机
代码:
private void ScaleBaseMousePos()
{
//缩放前鼠标位置对应的世界坐标 A点
Vector3 aimPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
//缩放前屏幕位置存储
Vector2 mousePos = Input.mousePosition;
//缩放
nowOriSize = Camera.main.orthographicSize - wheel * 50.0f;
Camera.main.orthographicSize = Mathf.Clamp(nowOriSize, minOriSize, maxOriSize);
//缩放后A点应该对应的屏幕坐标 A1点
Vector3 newMousePos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane));
transform.position += (aimPos - newMousePos);
}