为了获得精确值,可以在FixedUpdate里面执行,同时也可以减小计算量,更改FixedUpdate的更新速率的位置为Edit->Project Setting->Time。
float distance;
void start(){
distence=0;
}
void FixedUpdate{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
Vector2 touchDeltaPosition= Input.GetTouch(0).deltaPosition;
float temp=Mathf.Sqrt(Mathf.Pow(touchDeltaPosition.x,2f)+Mathf.Pow(touchDeltaPosition.y,2f));
// 路程
distence+=temp;
//速度
velocity=Mathf.Abs(temp/Time.deltaTime);
}
else{
distence=0;
}
}
*Mathf.Pow(float x1,float x2)计算x1的x2次方
*Mathf.Sqrt(float x1)计算x2的平方根