繁体   English   中英

在 y 轴上移动播放器

[英]Moving the player on the y-axis

我现在正在为 Android 开发 Pong-Clone,并观看了有关它的教程。 它与键盘完美配合,但我如何在手机上获得类似的控制。 我当前的代码如下所示:

    private void Start()
    {
        ball = GameObject.FindGameObjectWithTag("Ball").GetComponent<Ball>();
        col = GetComponent<BoxCollider2D>();
        if (side == Side.Left)
            forwardDirection = Vector2.right;
        else if (side == Side.Right)
            forwardDirection = Vector2.left;
    }

    private void Update()
    {
        if (!overridePosition)
            MovePaddle();
    }

    private void MovePaddle()
    {
        float targetYPosition = GetNewYPosition();
        ClampPosition(ref targetYPosition);
        transform.position = new Vector3(transform.position.x, targetYPosition, transform.position.z);
    }

    private void ClampPosition(ref float yPosition)
    {
        float minY = Camera.main.ScreenToWorldPoint(new Vector3(0, 0)).y;
        float maxY = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height)).y;
        yPosition = Mathf.Clamp(yPosition, minY, maxY);
    }

    private float GetNewYPosition()
    {
        float result = transform.position.y;
        if (isAI)
        {
            if (BallIncoming())
            {
                if (firstIncoming)
                {
                    firstIncoming = false;
                    randomYOffset = GetRandomOffset();
                }
                result = Mathf.MoveTowards(transform.position.y, ball.transform.position.y + randomYOffset, moveSpeed * Time.deltaTime);
            }
            else
            {
                firstIncoming = true;
            }
        }
        else
        {
            float movement = Input.GetAxisRaw("Vertical " + side.ToString()) * moveSpeed * Time.deltaTime;
            result = transform.position.y + movement;
        }
        return result;
    }

我希望玩家在 y 轴上移动,但我不知道如何使用触摸控制来做到这一点(我是游戏新手)。 我真的很感激任何帮助,所以我可以继续编程:)

如果您想按住使 object 移动,您可以制作两个按钮,一个用于 +y,另一个用于 -y。 然后使用Input.GetTouch注册玩家输入。

我将通过触摸并向上或向下拖动来假设你想要什么。

我会保留您的代码以进行调试,并添加一个开关以仅在支持触摸时使用触摸,否则使用您的代码。

// Adjust via the Inspector how much the user has to drag in order to move the padel
[SerializeField] private float touchDragSensitivity = 1;
private float lastTouchPosY;

// Make sure to store the camera
// Using Camera.main is quite expensive!
[SerializeField] private Camera _mainCamera;

private void Awake()
{
    if(!_mainCamera) _mainCamera = GetComponent<Camera>();

    ...
}

private float GetNewPosition()
{
    ...

    else
    {
        var movement = 0f;
        if(!Input.touchSupported)
        {
            // For non-touch device keep your current code
            movement = Input.GetAxisRaw("Vertical " + side.ToString()) * moveSpeed * Time.deltaTime;
        }
        else
        {
            if(Input.touchCount > 0)
            {
                var touch = Input.GetTouch(0);
                var touchPositionY = _mainCamera.ScreenToWorldPoint(touch.position).y;
                if(touch.phase = TouchPhase.Moved)
                {
                    var delta = touchPositionY - lastTouchPosY;
                    movement = delta * touchDragSensitivity;
                }

                lastTouchPosY = touchPositionY;
            }
        }
        result = transform.position.y + movement;
    }
}

或者,如果您希望获得与按钮输入类似的体验,您也可以只检查触摸是在屏幕的下半部分还是上半部分,并像之前的原始代码一样不断移动 padel:

private void Update()
{
    ...

    else
    {
        var movement = 0f;
        if(!Input.touchSupported)
        {
            // For non-touch device keep your current code
            movement = Input.GetAxisRaw("Vertical " + side.ToString()) * moveSpeed * Time.deltaTime;
        }
        else
        {
            if(Input.touchCount > 0)
            {
                var touch = Input.GetTouch(0);
                var touchPositionY = touch.position.y;
                movement = (touchPositionY >= Screen.height / 2f ? 1 : -1) * moveSpeed * Time.deltaTime;
            }
        }
        result = transform.position.y + movement;
    }
}

更新

至于你的问题

有什么办法可以分割屏幕让两个玩家可以在同一个屏幕上玩

是的:您可以使用touch.position.x >= screen.width / 2f => 右侧添加检查触摸在哪一侧。

例如,您可以过滤有效的触摸,例如

if(Input.touchCount > 0)
{
    if(side == Side.Right)
    {
        var validTouches = Input.touches.Where(t => t.position >= Screen.width / 2f).ToArray();
        if(validTouches.Length > 0)
        {
            var touchPositionY = validTouches[0].position.y;
       
            movement = (touchPositionY >= Screen.height / 2f ? 1 : -1) * moveSpeed * Time.deltaTime;
        }
    }
    else
    {
        var validTouches = Input.touches.Where(t => t.position < Screen.width / 2f).ToArray();
        if(validTouches.Length > 0)
        {
            var touchPositionY = validTouches[0].position.y;
       
            movement = (touchPositionY >= Screen.height / 2f ? 1 : -1) * moveSpeed * Time.deltaTime;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM