c# - Unity 中逼真的阀轮旋转
问题描述
我正在 Unity 中进行逼真的管道阀轮旋转。这是我已经拥有的:
[SerializeField] private float cur_HP;
private const float max_HP = 100;
private Vector3 PrevmousePos;
private bool SpaceIsPressed;
// Start is called before the first frame update
void Start()
{
cur_HP = max_HP;
}
// Update is called once per frame
void Update()
{
SpaceIsPressed = Input.GetKey(KeyCode.Space);
Wheel();
}
void Wheel()
{
Vector3 mouseDelta = Input.mousePosition - PrevmousePos;
if (mouseDelta.x > 0 && SpaceIsPressed)
{
float amount = 0.01f;
cur_HP -= mouseDelta.x * amount;
} else if(mouseDelta.x < 0 && SpaceIsPressed)
{
float amount = 0.01f;
cur_HP += mouseDelta.x * amount;
}
if (cur_HP <= 0)
{
cur_HP = 0;
Debug.Log("You have unlocked");
}
if (cur_HP > 100)
{
cur_HP = 100;
}
transform.localRotation = Quaternion.Euler(0, 0, (720 / max_HP * cur_HP));
PrevmousePos = Input.mousePosition;
}
我现在想要什么。就像现实生活中的阀门管轮一样。如果你想松开它。你需要用很大的力才能向右旋转。然后越松开它就越容易旋转。如果你想收紧它,反之亦然。所以在我的帮助下,我的小脑袋想不出任何数学或代码。你们能给我提示或提示如何做吗?
编辑:这是关于我现在拥有的 gif,我正在使用鼠标右扫的空格键来旋转滚轮https://gyazo.com/004b2f8c4424476c796ae42ad28dacce
解决方案
所以你有cur_HP
which 应该设置阀门旋转多少,它是用 设置的mouseDelta.x
。
您现在希望下一个修改 ( mouseDelta.x
) 更小,更cur_HP
接近max_HP
:
cur_HP += (mouseDelta.x * max_HP/cur_HP) * amount;
这将使车轮越接近终点,线性速度就越慢。
您可能需要调整该amount
变量,因为当旋转 1 度时,车轮现在将旋转 100 倍。
确保在 cur_HP 为 0 时进行处理,因为除以零是非常非法的。
示例,相同的 X 运动,不同的松紧度:
紧密度 20%
- cur_HP = 20
- 最大生命值 = 100
- mouseDelta.x = 1
- 数量 = 0.1f
--> 新 cur_HP = 20.5
紧密度:60%
- cur_HP = 60
- 最大生命值 = 100
- mouseDelta.x = 1
- 数量 = 0.1f
--> 新 cur_HP = 60.16666
推荐阅读
- python - 如何使用 image_to_string 从包含不同语言文本的图像中提取文本?
- javascript - CS50 网络 - 项目 4 网络
- javascript - 想要获取复选框的值并将其值分配给类名
- reactjs - 为什么我们在 styorybook/react 中需要 ComponentStory、ComponentMeta
- angular - 从源“http://localhost:4200”访问“localhost:8088/user/”处的 XMLHttpRequest 已被 CORS 策略阻止
- google-apps-script - 从 Google App Script 中的 doPost 返回 500 错误
- graph-algorithm - 如何调整浊点以满足最小距离要求?
- angular - Windows 角度需要创建带有 CRLF 结尾的文件
- mysql - 在执行更新中出错,表中没有来自 column_list 的值的分区
- facebook - 无法使用 Facebook API 创建广告