C# unity玩家用手指在x轴上移动不起作用
嘿,我正在开发一款3d统一的android游戏,重点是你是一个正方形,你必须在x轴上移动。我希望玩家可以将他的手指放在任何他想要的地方,向左或向右滑动(仍在触摸显示器),并且他开始触摸的位置和他现在所在位置之间的距离,正方形应该向右或向左移动。当玩家不接触时,它不应在x轴上移动。我这样做了,但我的代码有一个问题,当我释放我的手指和触摸再次没有移动的权利或左侧的广场偏转到一边非常快。当然,当手指不移动时,正方形不应该移动C# unity玩家用手指在x轴上移动不起作用,c#,android,unity3d,controls,touch,C#,Android,Unity3d,Controls,Touch,嘿,我正在开发一款3d统一的android游戏,重点是你是一个正方形,你必须在x轴上移动。我希望玩家可以将他的手指放在任何他想要的地方,向左或向右滑动(仍在触摸显示器),并且他开始触摸的位置和他现在所在位置之间的距离,正方形应该向右或向左移动。当玩家不接触时,它不应在x轴上移动。我这样做了,但我的代码有一个问题,当我释放我的手指和触摸再次没有移动的权利或左侧的广场偏转到一边非常快。当然,当手指不移动时,正方形不应该移动 是否有人知道这个问题,或者有其他解决方案可以让我移动播放器,如上所述这里我
是否有人知道这个问题,或者有其他解决方案可以让我移动播放器,如上所述这里我找到了一个没有这些问题的更好的代码,我希望这可以帮助其他程序员;)
为什么你总是做
touchedPosMoved
而不是只在TouchPhase.Moved中?另外,使用ScreenToWorldPoint
可能不是最好的选择。。您可以直接使用输入差乘以某个乘数。。。
// My Code
public class PlayerMovement : MonoBehaviour
{
void FixedUpdate()
{
// move the player constantly forward
transform.position += Vector3.forward * Time.deltaTime * speed;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
// the current finger position
touchedPosMoved = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
switch (touch.phase)
{
case TouchPhase.Began:
// get finger position when touch start
touchedPosBegan = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
startX = transform.position.x;
break;
case TouchPhase.Moved:
// claculate the distance between start and curent position of the finger
differenz = Mathf.Abs(touchedPosBegan.x - touchedPosMoved.x);
if (touchedPosBegan.x > touchedPosMoved.x)
{
differenz = differenz * -1;
}
break;
}
// Move player at the X-axis
Vector3 idk = new Vector3((startX + differenz) * 8, transform.position.y, transform.position.z);
gameObjectStart = new Vector3(startX, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(gameObjectStart, idk, Time.deltaTime * 2);
}
}
}
private void FixedUpdate()
{
transform.position += Vector3.forward * Time.deltaTime * speed;
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
transform.position = new Vector3(
transform.position.x + touch.deltaPosition.x * multiplier,
transform.position.y,
transform.position.z + touch.deltaPosition.y * multiplier);
}
}
}