Unity Gyroscope function

Source: Internet
Author: User



Realize gyroscope function, rotate device, camera follow rotation

Drag the following script to the camera, pack it as an Android or iOS project, and test on the real machine

You should put some models in the scene, or you won't see the effect.



Using Unityengine;  
  
Using System.Collections;  
    public class Aaa:monobehaviour {Private Const float lowpassfilterfactor = 0.2f;  
        protected void Start () {//Set the device gyroscope on/off state, the gyroscope function must be set to true Input.gyro.enabled = true;  
        Get the device of gravitational acceleration vector Vector3 devicegravity = Input.gyro.gravity;  
        The rotation speed of the device, the return result is the rotational speed of the x,y,z axis, the unit is (radians/sec) Vector3 rotationvelocity = Input.gyro.rotationRate;  
        Obtain more precise rotational Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;  
        Set up a gyroscope update retrieval time, that is, 0.1 seconds update Input.gyro.updateInterval = 0.1f;  
    Obtain the acceleration Vector3 acceleration = Input.gyro.userAcceleration of the device after removing the acceleration of gravity; The protected void Update () {//input.gyro.attitude return value is of type quaternion, that is, the device rotates Euler angle transform.  
    rotation = Quaternion.slerp (transform.rotation, Input.gyro.attitude, lowpassfilterfactor); } void Ongui () {GUI. Label (New RecT (A, M, v), "Label:" + input.gyro.attitude.x + "" + Input.gyro.attitude.y + "" + Input.gyro.att  
    ITUDE.Z);   }  
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.