【问题标题】:Realistic Valve Wheel Rotation in UnityUnity 中逼真的阀轮旋转
【发布时间】:2019-05-15 14:55:56
【问题描述】:

我正在 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

【问题讨论】:

  • 所以,基本上,它应该越向右旋转越少?最大 720 度?
  • 向右旋转应该旋转得越少。就像你知道的真正的管阀轮一样。如果你曾经用螺丝刀拧过螺丝?你越紧它就会变得越来越难。就像那样@FredrikSchön
  • 线性?或者就在最后,就像螺丝刀一样?
  • 我认为是线性的。因为拧得越紧,旋转就越困难。反之亦然。顺便说一句,720 度是最大值

标签: c# unity3d


【解决方案1】:

所以你有cur_HP,它应该设置阀门旋转多少,它是用mouseDelta.x设置的。

您现在希望下一个修改 (mouseDelta.x) 更小,cur_HPmax_HP 越接近:

cur_HP += (mouseDelta.x * max_HP/cur_HP) * amount;

这将使轮子越接近终点线越慢。

您可能需要调整amount 变量,因为当旋转 1 度时,车轮现在会以 100 倍的速度旋转。

确保cur_HP为0时处理,因为除以零是非常非法的。


例如,相同的 X 运动,不同的松紧度:

紧密度 20%

  • cur_HP = 20
  • max_HP = 100
  • mouseDelta.x = 1
  • 金额 = 0.1f

--> 新的 cur_HP = 20.5

紧密度:60%

  • cur_HP = 60
  • max_HP = 100
  • mouseDelta.x = 1
  • 金额 = 0.1f

--> 新的 cur_HP = 60.16666

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多