Rotate object in Unity 3D

后端 未结 1 792
-上瘾入骨i
-上瘾入骨i 2020-12-10 14:49

I can use the following code to rotate object using accelerometer.

transform.rotation = Quaternion.LookRotation(Input.acceleration.normalized, Vector3.up);
<         


        
相关标签:
1条回答
  • 2020-12-10 15:21

    You can use transform.rotation like this:

    transform.rotation = new Quaternion(rotx, roty, rotz, rotw);
    

    OR

    You can use transform.Rotate like this:

    transform.Rotate(rotx, roty, rotz);
    

    Documentation for Quaternion

    Documentation for transform.rotation

    Example for Rotating screen with accelerometer input:

    float accelx, accely, accelz = 0;
    
    void Update ()
    {
        accelx = Input.acceleration.x;
        accely = Input.acceleration.y;
        accelz = Input.acceleration.z;
        transform.Rotate (accelx * Time.deltaTime, accely * Time.deltaTime, accelz * Time.deltaTime);
    }
    

    If you want to rotate the object to a specific angle use:

    float degrees = 90;
    Vector3 to = new Vector3(degrees, 0, 0);
    
    transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime);
    

    This will rotate 90 degrees around the x axis.

    0 讨论(0)
提交回复
热议问题