根据触摸移动速度减小比例

Decreasing scale depending on touch movement speed

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

我的脚本不起作用:

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);
        }
    }

例如,如果缓慢移动手指,对象将达到他的 scale.x == 0.3 5 秒,但如果他移动手指足够快,他将达到这个比例 3 秒。

您应该根据 Touch.deltaPosition 进行缩放,这是自上一帧以来触摸移动的增量。

The position delta since last change in pixel coordinates.

The absolute position of the touch is recorded periodically and available in the position property. The deltaPosition value is a Vector2 in pixel coordinates that represents the difference between the touch position recorded on the most recent update and that recorded on the previous update. The deltaTime value gives the time that elapsed between the previous and current updates; you can calculate the touch's speed of motion by dividing deltaPosition.magnitude by 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);
    }
}