unity3d中怎么实现让摄像机跟随手机的方向改变而改变 5
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏5(财富值+成长值)
3个回答
展开全部
用陀螺仪,非常方便,几行代码!
using UnityEngine;
using System.Collections;
// 陀螺仪
public class GyroscopeComponent: MonoBehaviour
{
public float interval = 0.2f;
Gyroscope gyro;
Quaternion quatMult;
Quaternion quatMap;
//UILabel ul;
public GameObject camParent;
private Vector3 qtBack;
private bool bDown = true;
void Start()
{
// find the current parent of the camera's transform
// instantiate a new transform
// match the transform to the camera position
camParent.transform.position = transform.position;
// make the new transform the parent of the camera transform
transform.parent = camParent.transform;
gyro = Input.gyro;
gyro.enabled = true;
camParent.transform.eulerAngles = new Vector3(90, 0, 0);
quatMult = new Quaternion(0, 0, 1, 0);
qtBack = transform.localRotation.eulerAngles;
}
void Update()
{
quatMap = new Quaternion(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
Quaternion qt = quatMap * quatMult;
transform.localRotation = qt;
}
}
using UnityEngine;
using System.Collections;
// 陀螺仪
public class GyroscopeComponent: MonoBehaviour
{
public float interval = 0.2f;
Gyroscope gyro;
Quaternion quatMult;
Quaternion quatMap;
//UILabel ul;
public GameObject camParent;
private Vector3 qtBack;
private bool bDown = true;
void Start()
{
// find the current parent of the camera's transform
// instantiate a new transform
// match the transform to the camera position
camParent.transform.position = transform.position;
// make the new transform the parent of the camera transform
transform.parent = camParent.transform;
gyro = Input.gyro;
gyro.enabled = true;
camParent.transform.eulerAngles = new Vector3(90, 0, 0);
quatMult = new Quaternion(0, 0, 1, 0);
qtBack = transform.localRotation.eulerAngles;
}
void Update()
{
quatMap = new Quaternion(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
Quaternion qt = quatMap * quatMult;
transform.localRotation = qt;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你能得到手机的方向么?
追问
已经解决,找到了相关脚本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询