Search code examples
androidunity-game-enginebox2dgame-enginegame-physics

Changlable body type in case of object type for collisions in Box2d?


I'm developing a game that ten balls which are BodyType.DynamicBody. And there are some obstacles which are BodyType.KinematicBody. The balls hit the obstacles and the balls rebound. This is OK. But the balls can hit each other in this way. I want that the balls can not hit each other and pass through each other.

To sum up, the balls will hit obstacle but not hit the other balls.

(If you know the game ballz or bbtan, I am trying the make similiar game of them)


Solution

    1. Create a new physics layer. Click where it says "layer" in the Inspector, click "Add Layer..." and name a layer (say, layer 12) to "Balls" (name isn't important).
    2. Set your balls to this physics layer. Select them all, click the "layer" dropdown in the Inspector, and select "Balls"
    3. Go to: Edit -> Project Settings -> Physics
    4. In the Inspector you should see a weird grid of checkboxes. Uncheck the box that is the intersection of "Balls" and "Balls"

    Physics settings

    Your balls will now not collide with each other.