简体   繁体   English

块滑动游戏的基于网格的运动

[英]Grid Based Movement for a Block Sliding Game

https://www.gamesmen.com.au/rush-hour-traffic-jam-logic-board-game https://www.gamesmen.com.au/rush-hour-traffic-jam-logic-board-game

there was this game that i try to recreate it in Unity.我尝试在 Unity 中重新创建这款游戏。 i've try to make the sliding blocks script, and it works.我尝试制作滑块脚本,并且它有效。 let's just say that i make it with a Grid, yes, that Grid where i can put tiles on it.假设我用网格制作它,是的,我可以在上面放置瓷砖的网格。 so how can i make the blocks snaps on the Grid?那么我怎样才能使块在网格上捕捉呢? especially if the block is more than 1 tile long?特别是如果块的长度超过 1 块?

they way how it works is that i use OnMouseDown at the moment because i've planned to built it on Android but now the problem is that i don't know how to make these blocks snap to grid with OnMouseUp.他们的工作方式是我目前使用 OnMouseDown,因为我计划在 Android 上构建它,但现在的问题是我不知道如何使用 OnMouseUp 使这些块对齐网格。

so far here's the Code for the Blocks到目前为止,这是块的代码

private void GrabABlock()
    {
        if (Input.GetMouseButton(0)) {
            Debug.Log("blockGrab"); 
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            BlockPosX = mousePos.x - this.transform.position.x;
            BlockPosY = mousePos.y - this.transform.position.y;
            Grabbed = true;
            if (Grabbed == true) {
                mousePos = Input.mousePosition;
                mousePos = Camera.main.ScreenToWorldPoint(mousePos);
                switch (BlockDirection)
                {
                    case BlockShape.Horizontal:
                        HorizontalMovement();
                        break;
                    case BlockShape.Vertical:
                        VerticalMovement();
                        break;
                    case BlockShape.Square:
                        OctaMovement();
                        break;
                }
            }
        }

    }

    private void LateUpdate() // Snap to Grid
    {
        mousePos.x = Mathf.Floor(gameObject.transform.position.x / 1f) * 1f;
        mousePos.y = Mathf.Floor(gameObject.transform.position.y / 1f) * 1f;
        pivot.transform.position = mousePos;
    }

my expectation is that the block's pivot detects the each tiles of the grid, and snaps into it onMouseUp我的期望是块的 pivot 检测到网格的每个图块,并在鼠标上捕捉到它

You can add a trigger collider to the block.您可以将触发对撞机添加到块中。 Then for every position in the grid add an empty GameObject, let's call it SnapLocation.然后为网格中的每个 position 添加一个空的 GameObject,我们称之为 SnapLocation。 Then OnMouseButtonUp you can move the blocks position to the SnapLocation's position.然后 OnMouseButtonUp 您可以将块 position 移动到 SnapLocation 的 position。

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

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