While there are games where win/lose conditions are not necessary, air hockey is definitely not one of them. In classic air hockey, there are two goal slots on the opposite sides of the table. If the puck gets into the goal of player 1, then player 2 gets a point and vice versa. The game ends when one of the players has 7 points.
There is the short-term win condition (score once) and the long-term win condition (score seven times before your opponent does). We will only implement the short-term win condition, although you are highly encouraged to try and implement the long-term one once you are done with this chapter.
Before we start implementing the win/lose conditions, let us make sure that the puck cannot get stuck in the corner, because this kind of behavior will prevent the win/lose condition from being triggered. Make four new walls, rotate them 45 degrees on the Y axis, and place them in the corners so as to get rid of the straight angles there, as shown in the next figure. This should solve the problem.
Now that this is taken care of, let us make the goal slots. Change the puck's Scale property to (1
, 0.7
, 1
) and place it in (-3
, 0.3
, 0
).
0
, 255
, 0
) and the cube on the right red (255
, 0
, 0
) by making new materials for them. Both of them should be in 2.5
on the Y axis. These are there to block the mallet, but not the puck.GoalTriggerLeft
and GoalTriggerRight
. Place them just behind the game field on either side of the table. These objects will act as triggers to detect which goal the puck has scored. Check the Is Trigger property of these game objects' Box Collider component. This property of the collider component makes sure that the object does not do any physics collisions (as opposed to the puck or the walls), but instead acts as a trigger, detecting when rigidbodies enter, stay inside, and exit it. These objects are the actual goals that will trigger the win/lose conditions.Scored
and State2 to LoadLevel
.Scene1
if you haven't changed anything. To make sure, look at the top of the Unity Editor window. It always says [SceneName].unity - [ProjectName] - [Platform]
, where the things in the brackets are Scene1, UPMTutorial, and Web Player for me, but can differ for you.Now, we can add some finishing touches to our scene by importing a 3D model:
WallVisual.fbx
file in the UnityPlaymakerTutorial
directory and click on Import. The file browser will close and a WallVisual
file will appear in the Project panel. Create a Models
folder and drag it there.WallVisual
file and have a look at Inspector. There are three tabs in this Inspector: Model, Rig, and Animations. These are all responsible for changing import settings of your model.1.05
and uncheck Import Materials. Leave the rest of the properties at their default values. Scale Factor changes the scale of the model on import, meaning that the Transform Scale property will be equal to (1
, 1
, 1
) while the actual size in 3D space may change. This is needed because the working scale in the 3D modeling software (such as Maya or 3DS Max) may differ from that in Unity. To make the model appear bigger, increase the scale factor. See the following screenshot to confirm that your import settings are set correctly:0
, 0.5
, -3.5
) and the second one in (0
, 0.5
, 3.5
). Set the first one's Rotation to (0
, 0
, 0
) and the second one's to (0
, 180
, 0
).Now, when you hit play, you should be able to score in both goals. Doing so will reload the currently loaded level. We also made sure that the puck does not get stuck in the corners of the table and imported a model that covers our cubic walls and makes the game look more like an actual air hockey table. This means that the game is finally playable, if, admittedly, not very fun yet.
18.117.145.173