在我的场景中,我有一个相机作为一个空的游戏对象的孩子。在我的脚本中,我能够围绕父对象旋转,翻译父对象,并通过在Z轴上移动它来缩放我的相机。
然而,我想要的是让父母移动时我的相机Y位置保持不变。在我下面发布的示例中,我已经研究了如何实现这一目标。但我不再能够放大和缩小了。
我已经看了一段时间了,而且我感觉有些蠢事,我没有做。但有人可以看看我的缩放和翻译方法,看看我做错了什么/失踪?
// Pinch fingers to zoom camera
// Will move camera on the Z access
void CameraZoom ()
{
// if fingers moved certain distance apart, zoom
float dot = Vector2.Dot (Input.GetTouch (0).deltaPosition.normalized, Input.GetTouch (1).deltaPosition.normalized);
if (dot < -fingerDistance)
{
// Store both touches.
Touch touchZero = Input.GetTouch (0);
Touch touchOne = Input.GetTouch (1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
if (useFieldOfView == true)
{
cam.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
// Clamp the field of view to make sure it's between 0 and 180.
cam.fieldOfView = Mathf.Clamp (cam.fieldOfView, 0.1f, 179.9f);
}
else if (useFieldOfView == false)
{
transform.position = new Vector3 (transform.position.x , transform.position.y , transform.position.z );
transform.Translate (0, 0, deltaMagnitudeDiff * perspectiveZoomSpeed);
}
}
}
void MoveCamera ()
{
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
// this is the line keeping my camera stationary on the Y axis
transform.position = new Vector3 (transform.position.x , cameraCurrentYHeight , transform.position.z );
transform.parent.Translate (-touchDeltaPosition.x * speed, 0, -touchDeltaPosition.y * speed);
if(transform.parent.position.y <= yMovementLowestValue)
{
transform.parent.position = new Vector3(transform.parent.position.x, yMovementLowestValue, transform.parent.position.z);
}
}
我最初的想法是在父母移动时拍摄相机当前高度的“快照”。然后,当父母移动时,只需将相机保持在这个恒定的高度。我通过检查已经注册了两个触摸并且它们是静止的,然后在调用移动,缩放和移动方法时执行此操作。所有这一切,都可以在我的更新方法中找到,如下所示:
if (Input.touchCount == 2 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (1).phase == TouchPhase.Moved)
{
CameraZoom ();
MoveCamera ();
takeSnapShot = false;
}
if(Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Stationary && Input.GetTouch(1).phase == TouchPhase.Stationary)
{
takeSnapShot = true;
}
if(takeSnapShot == true)
{
cameraCurrentYHeight = cam.transform.position.y;
}
答案 0 :(得分:1)
两个标准化向量的点积总是给你1
float dot = Vector2.Dot (Input.GetTouch (0).deltaPosition.normalized, Input.GetTouch (1).deltaPosition.normalized);
我认为你的意思是:
float dot = (Input.GetTouch (0).deltaPosition - Input.GetTouch (1).deltaPosition).sqrMagnitude;
此外,由于您使用的是平方幅度,我确定因为您关注性能,所以您希望在if
语句中将两个方块进行比较,如下所示:
if (dot < fingerDistance * fingerDistance)
答案 1 :(得分:0)
为什么不将相机移出emptyGameObject,只使用emptyGameObject x和z更新相机的x和z位置。
public GameObject control;
..
control.transform.Translate (-touchDeltaPosition.x * speed, 0, -touchDeltaPosition.y * speed);
transform.position = new Vector3(control.transform.position.x, transform.position.y, control.transform.position.z);