logo

Babylon.js Market

By Lawrence

5 minutes

One capsule on an empty pitch is not a game. You need players in red and blue. That means six AI capsules. Chasing and shooting come in the next two lessons. First, each midfielder needs to know its place on the pitch: which third of the field is home.

AIZone gives a capsule that home. The capsule moves freely inside a set area. When it wanders too far, the zone gently pulls it back. You add this one component to four of the six capsules.

Add the players

Four midfielders each get a zone centered on their starting third. The two strikers start as plain physics bodies. Their AI comes later, in Lesson 8. Every capsule has the ai tag, so BallPossession already counts them. Each midfielder also gets a team:Red or team:Blue tag:

src/scenes/soccer.json
"RedMidL": {
  "tags": ["ai", "team:Red"],
  "components": {
    "MeshPrimitive": { "primitive": "capsule", "radius": 0.5, "height": 2, "position": [-7, 1, 5], "material": { "diffuseColor": [0.8, 0.32, 0.32] } },
    "Shadow":  { "castShadow": true, "receiveShadow": false },
    "Physics": { "shapeType": "capsule", "motionType": "dynamic", "mass": 1, "lockRotation": true },
    "AIZone":  { "center": [-7, 0, 5], "radius": 9, "returnSpeed": 5 }
  }
},
"RedStriker": {
  "tags": ["ai"],
  "components": {
    "MeshPrimitive": { "primitive": "capsule", "radius": 0.5, "height": 2, "position": [-3, 1, 0], "material": { "diffuseColor": [0.95, 0.35, 0.35], "emissiveColor": [0.2, 0.04, 0.04] } },
    "Shadow":  { "castShadow": true, "receiveShadow": false },
    "Physics": { "shapeType": "capsule", "motionType": "dynamic", "mass": 1, "lockRotation": true }
  }
}

Add RedMidR (zone at [-7, 0, -5]), BlueMidL ([7, 0, 5]), and BlueMidR ([7, 0, -5]) as midfielders. Add BlueStriker (at [3, 1, 0]) as a plain body like RedStriker. The strikers have no AIZone. They will roam the whole pitch to hunt for the goal, so nothing holds them to one spot.

Continue reading

Unlock the Full Course

Every lesson, the runnable examples, and the finished build — yours to keep.

$9one-time

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search