我是新来的。我正在基于增强现实的项目中工作,并且正在该应用程序中播放3D动画。我想向应用程序添加一个功能,以“捏”和“捏”任何3D动画。我在3D动画缩进和缩进时遇到问题。我已经尝试过以统一形式提供的代码,但是正交相机存在问题。
统一站点代码
using UnityEngine;
public class PinchZoom : MonoBehaviour
{
public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode.
public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode.
void Update()
{
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// If the camera is orthographic...
if (camera.isOrthoGraphic)
{
// ... change the orthographic size based on the change in distance between the touches.
camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
// Make sure the orthographic size never drops below zero.
camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f);
}
else
{
// Otherwise change the field of view based on the change in distance between the touches.
camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
// Clamp the field of view to make sure it's between 0 and 180.
camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f);
}
}
}
}
我的尝试
using UnityEngine;
public class Pinch_MUN : MonoBehaviour {
public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode.
public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode.
void Update()
{
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// If the camera is orthographic...
if (GetComponent<Camera>().orthographic == true)
{
// ... change the orthographic size based on the change in distance between the touches.
GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
// Make sure the orthographic size never drops below zero.
GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
}
else
{
// Otherwise change the field of view based on the change in distance between the touches.
GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
// Clamp the field of view to make sure it's between 0 and 180.
GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 0.1f, 179.9f);
}
}
}
}
对我来说无关紧要,无论是捏照相机还是只捏视图中的3D动画。 根据上述情况,请帮助我找出我的错误。 在此先感谢您的帮助。
答案 0 :(得分:0)
请使用现成的解决方案,而不是编写自己的手势检测逻辑(我在一些项目中使用了TouchKit)。在AR中,您无法修改摄像机属性(位置或 fov),因为这会使您的虚拟摄像机与物理摄像机不同步。您应该逐步缩放游戏对象以调整大小。