【问题标题】:Problem in pinch in and pinch out of 3D Animation in unity统一捏合和捏出 3D 动画的问题
【发布时间】:2018-11-23 03:50:36
【问题描述】:

我是新来的。我正在开发一个基于增强现实的项目,我正在该应用程序中播放 3D 动画。我想为任何 3D 动画的捏合和捏合应用程序添加一个功能。我在捏合和捏出 3D 动画时遇到问题。我已经尝试过以统一形式提供的代码,但正交相机存在问题。

Unity 站点代码

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 动画,这对我来说并不重要。 请根据上述情况帮助我找出我的错误。 提前感谢您的帮助。

【问题讨论】:

    标签: c# unity3d animation 3d maya


    【解决方案1】:

    不要编写自己的手势检测逻辑,而是使用现成的解决方案(我在一些项目中使用过TouchKit)。在 AR 中,您无法修改相机属性(位置 fov),因为它会使您的虚拟相机与物理相机不同步。您应该逐步向上和向下缩放游戏对象以调整大小。

    【讨论】:

    • 谢谢,但这有助于我放大和缩小游戏对象?
    • 呃...是的吗?
    • 实现起来有点困难。
    • 是的,虽然没有那么多,这就是为什么你会得到成绩或金钱来做这件事。在 OK 中提出关于 SO 的问题,而不是让人们为你做你的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2017-05-15
    • 2015-09-28
    • 2012-07-25
    • 2013-07-11
    • 2023-03-28
    相关资源
    最近更新 更多