我想在整个屏幕上左右移动字符,如果我从任意位置向左或向右移动,则只需要移动我现在有多少= 1.5f
我有这段代码并且可以完美地工作,但是它仅适用于鼠标,而不适用于触摸屏:
if (!isMoving && Input.GetMouseButtonDown(0))
{
desiredPos = transform.position + Vector3.right * 1.52f;
isMoving = true;
}
if (!isMoving && Input.GetMouseButtonDown(1))
{
desiredPos = transform.position - Vector3.right * 1.52f;
isMoving = true;
}
if (isMoving)
{
transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);
// this == is true if the difference between both
// vectors is smaller than 0.00001
if (transform.position == desiredPos)
{
isMoving = false;
// So in order to eliminate any remaining difference
// make sure to set it to the correct target position
transform.position = desiredPos;
}
}
我尝试实现此代码,但这不起作用
private void Update()
{
timer += Time.deltaTime;
int i = 0;
//loop over every touch found
while (i < Input.touchCount)
{
if (!isMoving && Input.GetTouch(i).position.x > ScreenWidth / 2)
{
//move right
desiredPos = transform.position + Vector3.right * 1.52f;
isMoving = true;
}
if (!isMoving && Input.GetTouch(i).position.x < ScreenWidth / 2)
{
//move left
desiredPos = transform.position - Vector3.right * 1.52f;
isMoving = true;
}
++i;
}
if (isMoving)
{
transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);
// this == is true if the difference between both
// vectors is smaller than 0.00001
if (transform.position == desiredPos)
{
isMoving = false;
// So in order to eliminate any remaining difference
// make sure to set it to the correct target position
transform.position = desiredPos;
}
}
}
第二个代码向左或向右移动字符太多,我需要像第一个代码中一样完全移动,但触摸向左或向右滑动。
我想要这样的东西精确地移动1.5f
答案 0 :(得分:1)
要与GetMouseButtonDown
保持一致,还需要检查触摸的短语:
Input.GetTouch(i).phrase == TouchPhase.Began