思路:
首先确定Sensor上报数据控制的是摄像头角度旋转
所以限定摄像头旋转角度的脚本要挂在摄像头上
确定视角边界
查看效果
using UnityEngine;
using System.Collections;
public class cameraControl : MonoBehaviour {
private Vector3 euler;
private Vector2 startPos;
private Vector2 startPospa;
private string uilog;
private bool displayUI;
private float timer;
private Quaternion originalRotation;
private Quaternion yLeftBorderRotation;
private Quaternion yRightBorderRotation;
private Quaternion xDownBorderRotation;
private Quaternion xUptBorderRotation;
// Use this for initialization
void Start () {
displayUI = false;
originalRotation=transform.rotation;
yLeftBorderRotation = Quaternion.Euler (0.25f, 336.5f, 0f);
yRightBorderRotation = Quaternion.Euler (359.25f, 28f,0f);
xDownBorderRotation = Quaternion.Euler (13f, 359.75f,0f);
xUptBorderRotation = Quaternion.Euler (342.25f, 1.5f,0f);
Debug.Log ("Martin "+originalRotation.x+","+originalRotation.y+","+originalRotation.z+",");
}
// Update is called once per frame
void Update () {
//update rotate
rotateUpdate ();
//timer
if ((Time.time - timer) > 2.0f) {
displayUI = false;
}
}
//rotate update
void rotateUpdate()
{
float x=transform.rotation.eulerAngles.x;
float y=transform.rotation.eulerAngles.y;
Vector3 m= transform.eulerAngles;
Debug.Log("Martin Vector eulerAngles m.x :"+m.x);
Debug.Log("Martin Vector eulerAngles m.x :"+m.y);
Debug.Log("Martin Vector eulerAngles m.x :"+m.z);
Debug.Log("Martin x :"+x);
Debug.Log("Martin y :"+y);
if(y<336.5f && x>0.26f){
transform.rotation=yLeftBorderRotation;
}else if(y>28f && x<360f){
transform.rotation=yRightBorderRotation;
}else if(x>13f && y<360f){
transform.rotation=xDownBorderRotation;
}else if(x<343f && y>1.5f){
transform.rotation=xUptBorderRotation;
}
#if !UNITY_ANDROID || UNITY_EDITOR
if(Input.GetMouseButton(0))
{
euler.x -= Input.GetAxis ("Mouse Y") * 5;
euler.y += Input.GetAxis ("Mouse X") * 5;
Quaternion qua = Quaternion.Euler (euler.x, euler.y,0f);
transform.rotation = qua;
//Quaternion originalRotation=transform.rotation;
// float m=transform.Rotate.x
}
#else
if (Input.touchCount > 0) {
var touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
startPospa = touch.position;
break;
// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
Vector2 delta = touch.position - startPos;
euler.x -= delta.y*0.1f;
euler.y += delta.x*0.1f;
Debug.Log("tclogposition " + delta.x + " " + delta.y);
Quaternion qua = Quaternion.Euler (euler.x, euler.y,0f);
transform.rotation = qua;
startPos = touch.position;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
Vector2 deltaa = touch.position - startPospa;
if(deltaa.magnitude < 0.01f)
{
doclick();
displayUI = true;
timer = Time.time;
}
break;
}
}
#endif
}
private bool isVisible = true;
void doclick()
{
uilog = "pa pa pa ...";
Debug.Log ("pa pa pa ...");
OVRManager.eventFromSensor = keyEvent.Enter;
// OVRManager.eventFromSensor = keyEvent.Enter;
}
// void OnGUI()
// {
// if (displayUI) {
// GUI.Label (new Rect(300f,200f,200f,100f), uilog);
// }
//
// }
}