【问题标题】:Moving the player on the y-axis在 y 轴上移动播放器
【发布时间】:2020-12-01 15:32:51
【问题描述】:

我现在正在为 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 轴上移动,但我不知道如何使用触控来实现(我是游戏新手)。我真的很感激任何帮助,所以我可以继续编程:)

【问题讨论】:

    标签: c# android unity3d unity3d-2dtools


    【解决方案1】:

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

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

    // 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 &gt;= 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;
            }
        }
    }
    

    【讨论】:

    • 第二种方法效果很好,但是有没有办法分割屏幕让两个玩家可以在同一个屏幕上玩?不管怎样,谢谢!
    • 是的 ;) 请参阅底部的更新。您可以简单地过滤掉仅使用屏幕左半部分或右半部分的触摸
    • 非常感谢,非常感谢您的帮助!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      相关资源
      最近更新 更多