当用户触摸屏幕并从屏幕上移开手指时,我将对象上移,
当用户触摸屏幕并按住手指在屏幕上时,我将对象移到右。
问题是,如果用户将手指从屏幕上移开,则对象仍在移动。
void FixedUpdate()
{
// Detect the first click
if (Input.GetMouseButtonDown(0))
{
totalDownTime = 0;
clicking = true;
}
// If a first click detected, and still clicking,
// move object right
if (clicking && Input.GetMouseButton(0))
{
totalDownTime += Time.deltaTime;
rigidBody.velocity = Vector2.zero;
if (totalDownTime >= 0.5f)
{
clicking = false;
OnLongClick.Invoke();
rigidBody.AddForce(Vector2.right * 200, ForceMode2D.Force);
}
}
// If a first click detected, and we release before the
// duration, move object up
if (clicking && Input.GetMouseButtonUp(0))
{
rigidBody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
clicking = false;
}
}
如何仅在触摸屏幕时移动对象?