如何减慢旋转速度?



所以我写了一些代码,如果我向左或向右滑动,使对象旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotater : MonoBehaviour {
public Transform player;
void Update()
{
if (Input.touchCount == 1)
{
// GET TOUCH 0
Touch touch0 = Input.GetTouch(0);
// APPLY ROTATION
if (touch0.phase == TouchPhase.Moved)
{
player.transform.Rotate(0f, 0f, touch0.deltaPosition.x);
}
}
}
}

问题是当我快速滑动时,旋转将无法控制。所以我希望输入不那么敏感。

我的目标是让旋转像滚动漩涡一样

我的设置:

  • 我做了一个空的物体,把它放在中间

  • 使空对象成为我的播放器的父对象

  • 最后,我将代码放在空对象中

这种设置使玩家在某种轨道上旋转,就像我告诉你的,类似于Rolly Vortex。

首先,您希望能够缩放灵敏度。这意味着对于触摸位置的每个变化单位,您将在旋转中获得一个单位变化的倍数。为此,创建一个可配置的(公共(成员变量public float touchSensitivityScale,并将旋转乘以该值。例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotater : MonoBehaviour {
public Transform player;
public float touchSensitivityScale;
void Update()
{
if (Input.touchCount == 1)
{
// GET TOUCH 0
Touch touch0 = Input.GetTouch(0);
// APPLY ROTATION
if (touch0.phase == TouchPhase.Moved)
{
player.transform.Rotate(0f, 0f, touch0.deltaPosition.x * touchSensitivityScale);
}
}
}
}

现在,您可以在检查器中编辑触摸灵敏度。将touchSensitivityScale设置为 1 时,行为将与当前行为相同。如果将数字设为 0.5,则旋转的灵敏度将减半。

如果这不能完全解决问题,并且您还需要一些平滑或加速,则可能需要对问题进行编辑。

我希望它有所帮助!

与其通过 touch0.deltaPosition.x 旋转,你总是可以有某种负指数函数。在这种情况下,它可能是类似于 e^(-x-a( 的东西,其中 x 是你的 touch0.deltaPosition.x,而 a 是一个变量,你必须根据你想要的初始旋转速度来确定。如果您不熟悉指数函数,请尝试使用 Desmos 等绘图软件绘制 y=e^(-x-a( 并改变 a 的值。一旦你想象它应该是不言自明的。

最新更新