且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Unity 2D游戏怪异碰撞的bug

更新时间:2021-12-14 16:25:56

嗯,首先,从 FixedUpdate 移动你的输入代码更新,否则它会导致应用laggy行为。第二件事情是,你可以通过创建 PhysicsMaterial2D 做到这一点摩擦= 0 反弹力= 0 ,并在材料$ C连接到播放器以及墙壁碰撞$ C>。 。希望这可以帮助你。

Well, first of all move your input code from FixedUpdate to Update otherwise it leads app to laggy behaviour. Second thing is you can do this by creating PhysicsMaterial2D with Friction = 0 and Bounciness = 0 and attach it to the player as well as walls collider in Material. Hope this helps you.

编辑:

下面是一个替代解决方案你,而是采用每块1盒对撞机,使用每方只有1对撞机。 4撞机总。

Here is an alternative solution for you, instead of using 1 box collider per block, use only 1 collider per side. 4 Colliders in total.

下面是代码,您可以将其添加到您的 BoardManager 类。并在 SetUpScene 方法的末尾调用它,您可以进一步修改它。

Here is the code, you can add it to your BoardManager class. And call it in at the end of SetUpScene method, you can further modify it.

void CreateWallsColliders ()
        {
            GameObject colliders = new GameObject ("Colliders");
            colliders.transform.position = Vector3.zero;

            GameObject leftCollider = new GameObject ("LeftCollider");
            leftCollider.transform.position = Vector3.zero;
            BoxCollider2D bcLeftCollider = leftCollider.AddComponent<BoxCollider2D> ();
            leftCollider.transform.parent = colliders.transform;

            GameObject rightCollider = new GameObject ("RightCollider");
            rightCollider.transform.position = Vector3.zero;
            BoxCollider2D bcRightCollider = rightCollider.AddComponent<BoxCollider2D> ();
            rightCollider.transform.parent = colliders.transform;

            GameObject topCollider = new GameObject ("TopCollider");
            topCollider.transform.position = Vector3.zero;
            BoxCollider2D bcTopCollider = topCollider.AddComponent<BoxCollider2D> ();
            topCollider.transform.parent = colliders.transform;

            GameObject bottomCollider = new GameObject ("BottomCollider");
            bottomCollider.transform.position = Vector3.zero;
            BoxCollider2D bcBottomCollider = bottomCollider.AddComponent<BoxCollider2D> ();
            bottomCollider.transform.parent = colliders.transform;

            // Assuming 15 x 15 tiles. Make it dynamic if you need.
            // Assuming -1 and 15 are the limits on both sides

            int rows = 15;
            int cols = 15;

            int lowerLimit = -1;
            int upperLimit = 15;

            leftCollider.transform.position = new Vector3 (lowerLimit, rows / 2);
            leftCollider.transform.localScale = new Vector3 (1, cols, 1);

            rightCollider.transform.position = new Vector3 (upperLimit, rows / 2);
            rightCollider.transform.localScale = new Vector3 (1, cols, 1);

            topCollider.transform.position = new Vector3 (cols / 2, upperLimit);
            topCollider.transform.localScale = new Vector3 (rows, 1, 1);

            bottomCollider.transform.position = new Vector3 (cols / 2, lowerLimit);
            bottomCollider.transform.localScale = new Vector3 (rows, 1, 1);
        }