【问题标题】:Single object 3D viewer for Google Cardboard with Unity 5带有 Unity 5 的 Google Cardboard 单对象 3D 查看器
【发布时间】:2015-09-02 00:36:19
【问题描述】:

我正在尝试重新创建 Google Cardboard 应用的“展览”演示的功能。即从各个方面查看单个对象 - 向上看,您可以看到对象下方,向下看,您可以从上方查看,向左或向右,您可以从侧面看到它,然后再向后看。

我尝试了很多方法,例如将对象设为相机的子对象,并使用 transform.LookAt(target); 将相机保持在对象上,但它不起作用。

Unity5 新手,非常感谢任何帮助。


更新

使用来自 SmoothMouseLook 脚本 (http://pastebin.com/vMFkZJAm) 的代码,这是迄今为止我得到的最接近的,但它并没有真正起作用并且感觉太“失控”(对象一直在旋转而不是平滑地转动检查)并且比“展览”演示的可预测性要低得多。我的猜测是我把事情复杂化了。有人有什么想法吗?...

在相机(“主相机”)上附加它以保持对对象的关注:

 using UnityEngine;
 using System.Collections;

 public class LookAt : MonoBehaviour {
     public Transform target;

     void Update () {
         transform.LookAt(target);
     }
 }

在对象上,附加此脚本:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class SmoothMouseLook : MonoBehaviour
{   
    /*
     This script is used to average the mouse input over x 
     amount of frames in order to create a smooth mouselook.
     */

    //Mouse look sensitivity    
    public float sensitivityX = 1f;
    public float sensitivityY = 1f;


    //Default mouse sensitivity
    public float defaultSensX = 1f;
    public float defaultSensY = 1f;


    //Minimum angle you can look up
    public float minimumY = -60f;
    public float maximumY = 60f;

    //Minimum angle you can look up
    public float minimumX = -60f;
    public float maximumX = 60f;


    //Number of frames to be averaged, used for smoothing mouselook
    public int frameCounterX = 35;
    public int frameCounterY = 35;


    //Mouse rotation input
    private float rotationX = 0f;
    private float rotationY = 0f;


    //Used to calculate the rotation of this object
    private Quaternion xQuaternion;
    private Quaternion yQuaternion;
    private Quaternion originalRotation;


    //Array of rotations to be averaged
    private List<float> rotArrayX = new List<float> ();
    private List<float> rotArrayY = new List<float> ();


    void Start ()
    {
        //Lock/Hide cursor

        if (GetComponent<Rigidbody>())      
            GetComponent<Rigidbody>().freezeRotation = true;


        originalRotation = transform.localRotation;

    }


    void FixedUpdate () 
    {

        //Mouse/Camera Movement Smoothing:    
        //Average rotationX for smooth mouselook

        float rotAverageX = 0f;
        //rotationX += Camera.main.transform.eulerAngles.x * sensitivityX;
        //rotationX += Cardboard.SDK.HeadRotation.eulerAngles.x * sensitivityX;
        rotationX += Cardboard.SDK.HeadPose.Orientation.x * sensitivityX;

        rotationX = ClampAngle (rotationX, minimumX, maximumX);

        //Add the current rotation to the array, at the last position
        rotArrayX.Add (rotationX);

        //Reached max number of steps?  Remove the oldest rotation from the array
        if (rotArrayX.Count >= frameCounterX) {

            rotArrayX.RemoveAt (0);

        }

        //Add all of these rotations together
        for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++) {
            //Loop through the array
            rotAverageX += rotArrayX[i_counterX];
        }


        //Now divide by the number of rotations by the number of elements to get the average
        rotAverageX /= rotArrayX.Count;


        //Average rotationY, same process as above
        float rotAverageY = 0;
        //rotationY += Camera.main.transform.eulerAngles.y * sensitivityY;
        //rotationY += Cardboard.SDK.HeadRotation.eulerAngles.y * sensitivityY;
        rotationY += Cardboard.SDK.HeadPose.Orientation.y * sensitivityY;

        rotationY = ClampAngle (rotationY, minimumY, maximumY);
        rotArrayY.Add (rotationY);


        if (rotArrayY.Count >= frameCounterY) {
            rotArrayY.RemoveAt (0);
        }


        for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++) {  
            rotAverageY += rotArrayY[i_counterY];
        }


        rotAverageY /= rotArrayY.Count;


        //Apply and rotate this object
        xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
        yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);

        transform.localRotation = originalRotation * xQuaternion * yQuaternion;

    }



    private float ClampAngle (float angle, float min, float max)
    {
        if (angle < -360f)  
            angle += 360f;
        if (angle > 360f)
            angle -= 360f;

        return Mathf.Clamp (angle, min, max);

    }
}

【问题讨论】:

    标签: unity3d scripting google-cardboard


    【解决方案1】:

    对于该特定用例,您不需要脚本。假设您使用的是 CardboardMain 预制件,请执行以下操作:

    • 将对象放在原点,CardboardMain 也放在原点。
    • 在 Cardboard 设置中,将 Neck Model Scale 设置为 0。
    • 打开 CardboardMain 并选择 Head 对象下的 Main Camera。
    • 将其变换位置 Z 值设置为负值(远到可以看到对象)。

    (您可以将其视为“自拍杆”相机模型。)

    【讨论】:

    • 好东西。我知道我把事情复杂化了!出于兴趣,您从哪里了解到 Unity 中的 Neck Model 和其他 Cardboard 设置?刚开始,只找到了基本的 SDK 文档(缺少示例),没有太多其他内容。
    • 好吧,我作弊了——我帮助编写了 SDK :-) 你说得对,文档仍然很少。
    • 感谢@smd 知道如何为对象旋转添加惯性,以便在您停止移动头部时慢慢停止?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多