Beispiel #1
0
    private void Update()
    {
        float hInput = InputManager.Instance.InputCtrl.HorizontalInput;
        float vInput = InputManager.Instance.InputCtrl.VerticalInput;

        float hSpeed = _player.GetHMoveSpeed();
        float vSpeed = _player.GetVMoveSpeed();

        float horizontalMove = hInput * hSpeed;
        float VerticalMove   = vInput * vSpeed;

        Vector2 playerPos = (Vector2)transform.position +
                            new Vector2(horizontalMove, VerticalMove) * Time.deltaTime;

        // player position is out of bound
        if (playerPos.x < m_moveAreaCentre.x - m_moveAreaShape.x / 2)
        {
            playerPos.x = m_moveAreaCentre.x - m_moveAreaShape.x / 2;
        }
        if (playerPos.x > m_moveAreaCentre.x + m_moveAreaShape.x / 2)
        {
            playerPos.x = m_moveAreaCentre.x + m_moveAreaShape.x / 2;
        }
        if (playerPos.y < m_moveAreaCentre.y - m_moveAreaShape.y / 2)
        {
            playerPos.y = m_moveAreaCentre.y - m_moveAreaShape.y / 2;
        }
        if (playerPos.y > m_moveAreaCentre.y + m_moveAreaShape.y / 2)
        {
            playerPos.y = m_moveAreaCentre.y + m_moveAreaShape.y / 2;
        }

        transform.position = new Vector3(playerPos.x, playerPos.y, transform.position.z);
    }