用手指在 x 轴上移动的统一播放器不起作用
unity player moving on the x axis with the finger does not work
嘿,我正在开发一个 android 游戏,它在 3d 中具有统一性,要点是你是一个正方形,你必须在 x 轴上移动。我希望玩家可以将手指放在他想要的任何地方并向左或向右滑动(仍然触摸显示屏)以及他开始触摸的位置和他现在所在位置之间的距离,方块应该向右或向左移动。当玩家不接触时,它不应该在 x 轴上移动。我这样做了,但是当我松开手指并再次触摸而不向右或向左移动时,我的代码出现了问题,正方形很快偏向一侧。当然手指不动的时候方块也不应该动。
Picture for better understand
// 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);
}
}
}
嘿,我正在开发一个 android 游戏,它在 3d 中具有统一性,要点是你是一个正方形,你必须在 x 轴上移动。我希望玩家可以将手指放在他想要的任何地方并向左或向右滑动(仍然触摸显示屏)以及他开始触摸的位置和他现在所在位置之间的距离,方块应该向右或向左移动。当玩家不接触时,它不应该在 x 轴上移动。我这样做了,但是当我松开手指并再次触摸而不向右或向左移动时,我的代码出现了问题,正方形很快偏向一侧。当然手指不动的时候方块也不应该动。
Picture for better understand
// 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);
}
}
}