C# 如何减缓旋转?
所以我写了一些代码,让物体在向左或向右滑动时旋转C# 如何减缓旋转?,c#,android,unity3d,game-engine,C#,Android,Unity3d,Game Engine,所以我写了一些代码,让物体在向左或向右滑动时旋转 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
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 touchsensitityscale
,并将旋转乘以该值。例如:
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);
}
}
}
}
现在,您可以在inspector中编辑触摸灵敏度。当TouchSensitityScale
设置为1时,行为将与当前相同。如果将数字设为0.5,则旋转的灵敏度将减半
如果这不能完全解决问题,并且您还需要一些平滑或加速,那么可能需要对问题进行编辑
我希望有帮助 与按touch0.deltaPosition.x旋转不同,您可以始终使用某种负指数函数。在这种情况下,它可能是沿着e^(-x-a)线的东西,其中x是touch0.deltaPosition.x,a是一个变量,你必须根据你想要的初始旋转速度来确定。如果您不熟悉指数函数,请尝试使用诸如Desmos之类的绘图软件绘制y=e^(-x-a)并改变a的值。一旦你意识到这应该是不言自明的 请不要让人们离开网站进入你的项目。请提供一个最简单可行的示例来解释您的问题。您是希望在刷卡后旋转速度逐渐减慢,还是希望在刷卡过程中输入的灵敏度降低?@Foggzie just Loss sensitive建议您使用绝对旋转而不是增量,因为错误可能会累积。谢谢您的帮助,但是不幸的是,这不起作用。是的,它减慢了旋转速度,但当我快速滑动时,它将无法控制地旋转。这里是正在发生的事情的视频,谢谢视频,这有点帮助。可能只是灵敏度太高了?你可以尝试0.05这样的值,看看它是否更接近(或补偿过度)。否则,我们可以尝试做一种反向加速,添加一个对数函数,使其在手指移动更快时,旋转速度接近某个极限。