[英]Start position gyroscope camera unity
I have a 360 video inside a sphere in my unity scene, and I'm using a gyro script so the user can rotate around and watch the movie, the problem is there is a person in the movie and I want the camera to start at the person's position, not the mobile's position, but the way I'm trying to do it is not working: 我在统一场景中的球体内部有一个360度视频,并且我使用的是陀螺仪脚本,因此用户可以旋转并观看电影,问题是电影中有人,并且我希望照相机从此处开始这个人的位置,而不是手机的位置,但是我尝试做的方法不起作用:
public class Gyro : MonoBehaviour
{
void Start()
{
if (SystemInfo.supportsGyroscope)
{
Input.gyro.enabled = true;
}
GameObject cameraParent = new GameObject("camParent");
cameraParent.transform.position = this.transform.position;
this.transform.parent = cameraParent.transform;
cameraParent.transform.Rotate(Vector3.right, 90);
}
void Update()
{
Quaternion cameraRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
this.transform.localRotation = cameraRotation;
}
}
SOLUTION 解
void Start()
{
Application.targetFrameRate = 60;
initialYAngle = transform.eulerAngles.y;
Input.gyro.enabled = true;
}
void Update()
{
if(calibra){
CalibrateYAngle();
calibra = false;
}
ApplyGyroRotation();
ApplyCalibration();
}
public void CalibrateYAngle()
{
calibrationYAngle = appliedGyroYAngle - initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time.
}
void ApplyGyroRotation()
{
transform.rotation = Input.gyro.attitude;
transform.Rotate(0f, 0f, 180f, Space.Self); // Swap "handedness" of quaternion from gyro.
transform.Rotate(90f, 180f, 0f, Space.World); // Rotate to make sense as a camera pointing out the back of your device.
appliedGyroYAngle = transform.eulerAngles.y; // Save the angle around y axis for use in calibration.
}
void ApplyCalibration()
{
transform.Rotate(0f, -calibrationYAngle, 0f, Space.World); // Rotates y angle back however much it deviated when calibrationYAngle was saved.
}
Quaternion cameraRotation = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
this.transform.localRotation = cameraRotation;
With that you only assign your giroscope position to your camera, if you want the camera to start from precise position, you need to add/substract the difference from the last frame and the current frame to your current position 这样,您只需将陀螺仪位置分配给摄像机,如果要使摄像机从精确位置开始,则需要添加/减去最后一帧和当前帧与当前位置之间的差
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.