logo

Babylon.js Market

By Lawrence

5 minutes

Your six AI capsules stand still in their zones. They should chase the ball. But if you send every midfielder straight at it, all four crowd into the same spot the moment the ball reaches a corner. They bunch up and shove each other instead of playing.

The fix is about position, not speed. On each team, only the closest midfielder runs for the ball. The rest stay put, and their zone keeps them home. One component named BallPursuit makes that choice. Then it sends a message that a component you already built is listening for.

Add ball pursuit and Movement to the midfielders

The four midfielders already have Physics, an AIZone, and a team tag. Give each one a Movement so it can walk, and a BallPursuit so it can decide when to run:

src/scenes/soccer.json
"RedMidL": {
  "tags": ["ai", "team:Red"],
  "components": {
    "Movement":    { "speed": 5, "groundY": 0 },
    "BallPursuit": { "targetEntity": "Ball", "chaseCount": 1 }
  }
}

Add the same Movement and BallPursuit block to RedMidR, BlueMidL, and BlueMidR. The strikers get nothing here. They go after the goal, not the ball. That is Lesson 8.

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