logo
By Lawrence

5 minutes

Strikers That Attack the Goal

The midfielders chase the ball, but it stays near the centre. Nobody is trying to score yet. Chasing the ball and attacking a goal are two different jobs.

Two components give each striker that second job. AIGoalSeek does the steering, getting behind the ball and pushing it toward the goal; AIKick does the shooting, picking a target and launching the ball. Both aim at the opponent's net.

Set up the strikers

The strikers came on in Lesson 6 as plain physics bodies. Now give each one a goal to chase and a net to shoot at. The red striker attacks Blue's net. The blue striker attacks Red's:

src/scenes/soccer.json
"RedStriker": {
  "tags": ["ai"],
  "components": {
    "AIGoalSeek": {
      "ballEntity": "Ball",
      "targetGoals": [{ "entityId": "BlueNet", "priority": 1 }],
      "avoidGoals": ["RedNet"],
      "speed": 5, "approachDistance": 2, "pushDistance": 1.5
    },
    "AIKick": {
      "ballEntity": "Ball", "targetGoals": ["BlueNet"], "teamEntities": ["RedMidL", "RedMidR"],
      "kickDistance": 1.8, "kickForce": 18, "kickCooldown": 0.8, "arcLift": 0.15, "waiveRangeCheck": true
    }
  }
}

Now set up BlueStriker the same way, flipped: targetGoals and kick targets become RedNet, avoidGoals becomes BlueNet, and teamEntities becomes ["BlueMidL", "BlueMidR"].

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