在y轴上移动播放器

问题描述

我现在正在开发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轴上移动,但是我不知道如何使用触摸控件来做到这一点(我是游戏新手)。我将非常感谢您的帮助,因此我可以继续进行编程:)

解决方法

如果要按住以使对象移动,则可以使两个按钮一个为+ y,另一个为-y。然后使用Input.GetTouch 注册玩家输入。

,

我将假设您想要通过触摸并向上或向下拖动来使Padel等化。

我将保留您的代码以进行调试,并添加一个开关,使其仅在支持touch的情况下才使用touch,否则将添加您的代码。

// 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;
    }
}

或者,如果您希望获得与按钮输入类似的体验,则还可以仅检查触摸是在屏幕的底部还是顶部,并像以前的原始代码一样不断移动键盘:

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;
        }
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...