2015-09-01 41 views
0

我正在嘗試重新創建Google Cardboard應用的「展示」演示的功能。即從各個角度觀看單個物體 - 向上看,看到物體下方,向下看,然後從上方觀看,向左或向右看,然後從側面看,然後再看。用於Google Cardboard with Unity 5的單個對象3D查看器

我已經嘗試了一些東西,比如讓對象成爲相機的一個孩子,並使用transform.LookAt(target);來保持相機專注於對象,但它不起作用。

新的Unity5,所以任何幫助將非常感激。


UPDATE

從SmoothMouseLook腳本(http://pastebin.com/vMFkZJAm),這是到目前爲止,我得到的最接近,但它並沒有真正的工作,感覺太「失控」(使用代碼該對象不停地旋轉而不是平穩地轉向檢查),並且比'Exhibit'演示更難以預測。我的猜測是我過於複雜的事情。任何人有任何的想法......

在相機(S)(「主相機」)附加至保持專注的對象:

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); 

    } 
} 

回答

3

對於那個特殊的用例,你不需要腳本。假設您使用的是CardboardMain預製件,請執行以下操作:

  • 將對象放在原點,CardboardMain也放在那裏。
  • 在Cardboard設置中,將Neck Model Scale設置爲0.
  • 打開CardboardMain並選擇Head對象下的Main Camera。
  • 將其設置爲將位置Z值轉換爲負值(足以查看對象)。

(你可以認爲這是「自拍 - 棒」的相機型號。)

+0

偉大的東西。我知道我過於複雜了!出於興趣,您在哪裏瞭解Unity中的Neck Model和其他Cardboard設置?剛開始,只找到基本的SDK文檔(缺少示例),沒有其他的東西。 – baroquedub

+0

嗯,我欺騙了一下 - 我幫助編寫了SDK :-)你說得對,文檔還是很少的。 – smd

+0

Thanks @smd任何想法如何爲物體旋轉添加慣性,以便在停止移動頭部時它會慢慢停下來? – robflate