Colliders and Collision Detection
Summary
Use the physics API to perform scene queries and manipulate entities using forces. Use collision filters and collision events to detect and control collisions. This tutorial expands on the previous player controller tutorial. You will use a physics scene query to determine if your avatar is on ground and can jump. Your avatar will shoot projectiles that apply an impulse to objects when a collision is detected. Collision filters will be configured to control which entities can collide and receive collision events. You will use a physics material to set the player's friction to zero and prevent sticking to walls.
Requirements
Setup
Start with the scene you created in Tutorial 2.
Create Collision Filters for Players, Bullets, and Terrain
Collision filters are used to assign entities to one or more collision groups and define how they interact with other groups. You can assign a collision filter to the ksEntityComponent and the collision filter will be used for all the colliders on the entity. Or you can assign a collision filter to a collider, which will override the collision filter on the ksEntityComponent. For more information on collision filters and collision events, see here.
Collision filters consist of three sets of flags: groups, notify, and collide.
Groups determines which collision groups the object belongs to. Objects may belong to multiple groups.
Collide determines which groups to physically collide with. In order for two entities to collide, they both must belong to groups that the other collides with.
Notify determines which groups will generate events when a collision or overlap occurs. If you set a filter to notify but NOT collide with a group, you will get
OverlapStartandOverlapEndevents as those entities pass through each other. Groups that do notify and collide will generateCollision,ContactUpdate, andContactLostevents. Only entities with the filter that is set to notify will receive events. So if entity 1 in group A is set to notify with group B, and entity 2 in group B is not set to notify with group A, entity 1 will receive events when the two entities collide, and entity 2 will not.
If a trigger collider is assigned a filter that has no notify flags set, it will receive overlap events for all groups.
Entities without a collision filter will use the default collision filter that belong to all groups, collide with all groups, and notify with none. You can change the default collision filter by assigning a different filter to the 'Default Collision Filter' in the ksPhysicsSettings inspector on the Room game object.
Open the 'Menu Bar->Reactor->Settings' menu. Expand the 'Collision Groups' section. This allows you to assign names to collision groups to be displayed in the editor. You can also assign collision group names when editing any collision filter assets.
Name the first three collision groups 'Player', 'Bullet', and 'Terrain'.
Create a collision filter asset by right-clicking in the project window and selecting 'Create->Reactor->Collision Filter' and name it 'Player'.
Assign the 'Player' filter to the 'Player' group, and set it to collide with 'Player' and 'Terrain'.
Create a collision filter named 'Bullet'.
Assign the 'Bullet' filter to the 'Bullet' group, and set it to notify with 'Player' and 'Terrain' and collide with nothing.
Create a collision filter named 'Terrain'.
Assign the 'Terrain' filter to the 'Terrain' group, and set it to collide with 'Player' and 'Terrain'.
Select the 'Platform' and all dynamic game objects. In the inspector for the ksEntityComponent, set the collision filter to 'Terrain'.
Select the 'Avatar' prefab and set the collision filter to 'Player'.

Create a Frictionless Physics Material
Physics materials control the friction and bounciness of rigid bodies. Friction causes the player's avatar to stick to walls when running into a wall. We will create a physics material with zero friction and assign it to the player's avatar to prevent it from sticking to walls. The player controller sets horizontal velocity to zero when no inputs are pressed to prevent sliding on the ground even when there is no friction.
Right-click in the project window and select 'Create->Physic Material' and name the material 'NoFriction'.
Set the 'Dynamic Friction' and 'Static Friction' to zero.
Set the 'Friction Combine' to 'Minimum'.
Select the 'Avatar' prefab and set the 'Physics Material' in the ksEntity inspector to the 'NoFriction' material you just created.
Create a Bullet Entity Prefab
Create a sphere.
Rename it to 'Bullet'.
Set the scale to (0.2, 0.2, 0.2).
Add a ksEntityComponent.
Add a Rigidbody component.
Set the collision filter to 'Bullet'.
Uncheck the 'Use Gravity' property in the Rigidbody component.
Make the bullet a prefab in the 'Resources' folder.
Delete the bullet from the hierarchy.
Scripting
Modify the Avatar Controller to Make it Shoot and Jump off the Ground
We will use a physics query to determine if the avatar is on the ground and can jump. There are three types of physics queries: sweeps, raycasts, and overlaps. We will use a sweep query below the avatar to see if we hit ground. See here for more information on how to use physics queries. We will also define and register a new shoot button input and use a delegate to handle shooting. We will add knockback velocity that is set when the player is hit by a bullet and apply it Update.
Open and edit AvatarController.cs from Tutorial 2.
AvatarController.cs
Modify the BindInputs Script to Bind Shoot Input
We need to bind our new button input to Unity input in our BindInputs script.
Open BindInputs.cs from Tutorial 2.
Modify the
Startfunction to bind Buttons.SHOOT to Unity axis "Fire1".
BindInputs.cs
Create a Server Bullet Script
The server bullet script destroys the bullet when it collides with something or after one second. It applies an impulse to entities it collides with.
Select the 'Bullet' prefab.
In the 'Add Component' menu, select 'Reactor->New Server Entity Script'.
Name the script 'ServerBullet'.
ServerBullet.cs
Modify the Server Room Script to Handle Shooting
The server room script assigns an OnShoot delegate to the controller to handle shooting.
Open ServerRoom.cs from Tutorial 2.
Add a const
BULLET_SPEEDIn the
SpawnAvatarfunction, make the controller'sOnShootdelegate spawn a bullet.
ServerRoom.cs
Testing
Build your scene config (CTRL + F2).
Start a local server.
Enter play mode.
You should only be able to jump while you are on the ground. You will shoot as long as you are holding down the mouse. Bullets will disappear when they collide with something, and will push around any dynamic cubes or other players they collide with.
Last updated

