在 Unity 3D 中旋转对象 [英] Rotate object in Unity 3D

查看:56
本文介绍了在 Unity 3D 中旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码使用加速度计旋转对象.

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

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

但我想旋转对象,例如屏幕正在旋转 - 0、90、180 和 360 度.我如何使用 Unity 3D 来做到这一点?

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?

推荐答案

你可以像这样使用 transform.rotation :

You can use transform.rotation like this:

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

你可以像这样使用 transform.Rotate:

transform.Rotate(rotx, roty, rotz);

四元数文档

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);

这将围绕 x 轴旋转 90 度.

This will rotate 90 degrees around the x axis.

这篇关于在 Unity 3D 中旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆