Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

unity3d - Rotate object in Unity 3D

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

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

But i would like to rotate object like for example screen is rotating - 0, 90, 180 and 360 degrees. How can I do it using Unity 3D?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...