Art*_*ski 5 3d rotation accelerometer unity-game-engine
我可以使用以下代码使用加速度计旋转对象.
transform.rotation = Quaternion.LookRotation(Input.acceleration.normalized, Vector3.up);
Run Code Online (Sandbox Code Playgroud)
但我想旋转对象,例如屏幕旋转 - 0度,90度,180度和360度.我怎么能用Unity 3D做到这一点?
小智 17
你可以transform.rotation像这样使用:
transform.rotation = new Quaternion(rotx, roty, rotz, rotw);
Run Code Online (Sandbox Code Playgroud)
要么
你可以transform.Rotate像这样使用:
transform.Rotate(rotx, roty, rotz);
Run Code Online (Sandbox Code Playgroud)
带加速度计输入的旋转屏幕示例:
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);
}
Run Code Online (Sandbox Code Playgroud)
如果要将对象旋转到特定角度,请使用:
float degrees = 90;
Vector3 to = new Vector3(degrees, 0, 0);
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime);
Run Code Online (Sandbox Code Playgroud)
这将围绕x轴旋转90度.
注意:如果你不明白什么transform.rotation是transform.Rotate你可以谷歌它像"unity 3d transform.eulerAngles",其中一个结果应该是统一文档.在开始使用之前阅读文档
小智 5
为了自己旋转你的游戏对象
int _rotationSpeed = 15;
void Update () {
// Rotation on y axis
// be sure to capitalize Rotate or you'll get errors
transform.Rotate(0, _rotationSpeed * Time.deltaTime, 0);
}
Run Code Online (Sandbox Code Playgroud)