Unity3D input.acceleration

问题描述 投票:0回答:1

我想用加速输入统一移动一个对象:

if (...)
transform.translate(vector2.right*speed);
else if (...)
transform.translate(vector2.left*speed);

我不知道应该是什么条件

unity3d unityscript
1个回答
2
投票

那么,你可以在技术上实现这一运动而不用任何条件。简单地在Update()或FixedUpdate()中添加类似的东西(建议使用FixedUpdate())。

Vector2 dir = Vector2.zero;
void FixedUpdate()
{
   dir.x = Input.acceleration.x;
   transform.translate(dir * speed * Time.deltaTime);
}

这是因为当您的手机平放在表面上时,Input.acceleration.x为0.如果它向右倾斜,则该值为正。如果向左倾斜,则值为负。从而让你摆脱条件限制。

© www.soinside.com 2019 - 2024. All rights reserved.