【问题标题】:Decreasing scale depending on touch movement speed根据触摸移动速度减小比例
【发布时间】:2020-07-03 09:54:53
【问题描述】:

我希望我的对象比例根据触摸移动速度减小。

这是我的脚本不起作用:

if (Input.touchCount > 0 && canRub)
    {
        Touch touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Moved)
        {
            scaleX -= transform.localScale.x / (strengthFactor * sM.durabilityForce);
            scaleY -= transform.localScale.y / (strengthFactor * sM.durabilityForce);
            scaleZ -= transform.localScale.z / (strengthFactor * sM.durabilityForce);
            transform.localScale = new Vector3(scaleX, scaleY, scaleZ);
        }
    }

例如,如果缓慢移动手指,物体将在 5 秒内达到他的 scale.x == 0.3,但如果他足够快地移动手指,他将在 3 秒内达到此比例。

【问题讨论】:

  • 那么sM.durabilityForce是什么?
  • 这只是另一个脚本中的一个变量,用于减缓规模下降

标签: c# unity3d touch scale


【解决方案1】:

您应该让缩放取决于Touch.deltaPosition,这是自上一帧以来触摸移动的增量。

自上次更改像素坐标以来的位置增量。

触摸的绝对位置会定期记录并在position 属性中可用。 deltaPosition 值是 像素坐标 中的 Vector2,表示最近更新记录的触摸位置与上次更新记录的触摸位置之间的差异。 deltaTime 值给出了上次和当前更新之间经过的时间; 您可以通过将deltaPosition.magnitude 除以deltaTime 来计算触摸的运动速度

所以你可以做类似的事情,例如

public float sensibility = 10;

if (Input.touchCount > 0 && canRub)
{
    Touch touch = Input.GetTouch(0);

    if (touch.phase == TouchPhase.Moved)
    {
        var moveSpeed = Touch.deltaPosition.magnitude / Time.deltaTime;
        // since moveSpeed is still in pixel space 
        // you need to adjust the sensibility according to your needs
        var factor = strengthFactor * sM.durabilityForce * moveSpeed * sensibility;

        scaleX -= transform.localScale.x / factor;
        scaleY -= transform.localScale.y / factor;
        scaleZ -= transform.localScale.z / factor;

        transform.localScale = new Vector3(scaleX, scaleY, scaleZ);
    }
}

【讨论】:

  • 好的,谢谢,我找到了相同的解决方案,所以它应该可以工作:3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多