For the complete documentation index, see llms.txt. This page is also available as Markdown.

Virtual Player Bots

Summary

Create bots using virtual players that can control entities using a player controller just like real players, except their inputs are set in server scripts instead of being sent over the network from a client. The virtual player bots will move in random directions. If they get out of bounds they will move back in bounds. They will use physics queries to find nearby players and will shoot at the nearest player. This tutorial uses Tutorial 3 or 5 as a base.

Requirements

Setup

  1. Start with the scene you created in Tutorial 3 (5).

Create a Static Cube in the Center of the Platform

The bots will not shoot at players if the static cube blocks their line-of-site to the player.

  1. Create a cube.

  2. Set the scale to (3, 3, 3).

  3. Set the position to (0, 2, 0).

  4. Add a ksEntityComponent.

  5. In the inspector for the ksEntityComponent, set the collision filter to 'Terrain'.

Scripting

Create a Class for Collision Constants

We will create a class to define constants for the collision groups we created in Tutorial 3.

  1. In 'Assets/ReactorScripts/Common', create a script named 'Collision'.

    • The collision consts values are based on the order the collision groups are defined int. The first collision group is value 1, the second is 1 << 1, the third is 1 << 2 and so on.

Collision.cs

Create a Server Bot Player script

The server bot player script runs bot logic and sets virtual input for virtual players. Virtual input is input set from server scripts that is used in player controllers for entities controlled by virtual players. Only virtual players have virtual input and can have their inputs set on the server.

The server bot script moves in random directions for random intervals. If it gets out of bounds, it will move back in bounds. It uses an overlap query to look for nearby players and picks the closest player it has line-of-site to to shoot at. It uses a raycast query for line-of-site checks.

  1. Select the 'Room' object.

  2. In the 'Add Component' menu, select 'Reactor->New Server Player Script'.

  3. Name the script 'ServerBot' and edit it as follows.

  4. Set the 'Bounds' property in the inspector to be a little bit smaller than the platform. Use (9, 9) if you are using the scene from Tutorial 3, or (24, 24) if you are using the scene from Tutorial (5.

ServerBotPlayer.cs

Modify the Server Room Script to Create Virtual Players

The server room script will create virtual players in Initialize.

  1. Open ServerRoom.cs from Tutorial 3.

  2. Add a NumBots field.

  3. Create virtual players to be bots in the Initialize function.

ServerRoom.cs

Testing

  1. Build your scene config (CTRL + F2).

  2. Start a local server.

  3. Enter play mode.

There will be 2 bots (or whatever number you set for 'Num Bots' in your 'ServerRoom' script) moving randomly around the platform. They will shoot at the closest player they have line-of-site to.

Last updated