Detecting goals
OK, we can throw the ball. Now, we need to detect if you got a goal and keep score. To detect a goal, we will add a Collider to the goal and handle any collision events from the ball. When you make a goal, you should be congratulated with audio and visual feedback and the points added to your score. First, let's detect whether you've made a goal. In the next section, we'll tackle keeping score.
Goal collider
To detect a goal, we will add a 3D Cube game object with a collider to the basket. It will only be an invisible collider, that is, not rendered as part of the scene. We want collisions to trigger events, which we'll handle in a script:
- With
BoxballGame
selected inHierarchy
, create a new 3D Cube, namedGoalCollider
. - Set its
Transform Position
to (0
,2.5
,0
) andScale
(1.8
,0.05
,1.8
). - On its
Box Collider
component, check theIs Trigger
checkbox. - Remove its
Mesh Renderer
component (gear-icon
|
Remove Component
). - The following screenshot shows the scene with
GoalCollider
before...