Unity 逼真的加速度计控制

发布于 2024-11-30 20:04:32 字数 495 浏览 0 评论 0原文

Vector3 acc = Vector3.Zero;
void update(){
    acc = Vector3.Lerp(acc, Input.acceleration, Time.deltaTime);
    transform.position = new Vector3(transform.position.x, (acc * -aVerticalRotation).x, transform.position.z);
}

我有上面的代码来垂直移动我的游戏对象(沿着 y 轴)。这工作正常,除了我希望对象相对于我的运动移动,即

如果我非常快地倾斜手机 X 量,它将移动距离 Z,而如果我倾斜手机 X 量但速度较慢,距离将为 A,其中 Z > ;答:

我尝试将当前读数从之前的读数中减去,然后将其与其余读数相乘,但除了引起抖动效果之外,它并没有完全按照我想要的方式工作。

有人可以帮助我实现这一目标吗?我对 Unity 还很陌生。

谢谢

Vector3 acc = Vector3.Zero;
void update(){
    acc = Vector3.Lerp(acc, Input.acceleration, Time.deltaTime);
    transform.position = new Vector3(transform.position.x, (acc * -aVerticalRotation).x, transform.position.z);
}

I have the above code to move my GameObject vertically (along the y-axis). This works fine except that I want the object to move relative to my motion i.e.

If I tilt the phone some X amount very fast it will move distance Z whereas if I tilt the phone with amount X but slower the distance will be A where Z > A.

I've tried to get the current reading subtracting it from the previous reading and multiplying it with the rest but apart from causing a jittery effect it didn't work exactly as I wanted it to.

Can some one help me achieve this ? I'm quite new to Unity.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2024-12-07 20:04:32

您的 lerp 代码不正确。

Lerp 根据随时间变化的开始和结束向量 3 返回向量 3 。是的,Input.acceleration 是 Vector3 数据类型,但它不是世界位置。它是加速度计的当前值。

您需要使用Input.acceleration值从当前的transform.position创建一个结束世界位置(假设该组件附加到您想要在场景中控制的对象)。然后,一旦获得该终点,将transform.position设置为等于Lerp结果。

根据您游戏的轴和设备方向等...加速度值可能无法将 X 映射到 X、Y 映射到 Y、Z 映射到 Z。您需要稍微尝试一下才能正确计算终点。

例如,我使用刚体物理,但我的加速度计 Y 控制我的 X 平移:

rigidbody.AddRelativeForce(
                Input.acceleration.y * StrafingSpeed,
                0.0f,
                0.0f,
                ForceMode.VelocityChange
            );

Your lerp code is incorrect.

Lerp returns a vector3 based on a start and end vector3 over time. Input.acceleration is a Vector3 data type, yes, but it is not a world position. It is the current values of the accelerometer.

You need to use the Input.acceleration values to create an end world position from your current transform.position (assuming this component is attached to the object you want controlled in the scene). Then once you have that end point, set transform.position equal to the Lerp result.

Based on your game's axis and device orientation etc... the acceleration values may not map X to X, Y to Y, Z to Z. You will need to play with it a little to get your end point calculating correctly.

For example, I use rigidbody physics, but my accelerometer Y controls my X translation:

rigidbody.AddRelativeForce(
                Input.acceleration.y * StrafingSpeed,
                0.0f,
                0.0f,
                ForceMode.VelocityChange
            );
德意的啸 2024-12-07 20:04:32

使用下面的代码来执行此操作。完美模拟加速度计。

rotFix = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
transform.localRotation = rotFix;

Using the below code to do this. Perfect simulate the accelerometers.

rotFix = new Quaternion(Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
transform.localRotation = rotFix;
鹿港小镇 2024-12-07 20:04:32

我建议你看看这里,这个应该能够帮助您了解如何使加速度计提供可用于控制速度的数字输入。希望这有帮助。

I would suggest you take a look here, this should be able to help you find out how to make the accelerometer give a numerical input that can be used to control speed. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文