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



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

这是我的脚本不起作用:

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

例如,如果缓慢移动手指,物体将达到他的刻度.x == 0.3 5秒,但如果他移动手指的速度足够快,他将达到这个刻度3秒。

您应该使缩放取决于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);
}
}

最新更新