2019-06-01 net

(Brent) #1

Unity


Top Adjust the physics
collider to match your
character’s shape
Above Adding the ground
objects to the game

Each time Update is called, the code will check if
the user has pressed the space bar. It will fire once
per press, so it will function like a button and won’t
continue to fire as we hold it down. We also check if
the plane is dead or not. If it is, we don’t want to allow
the player to control it any longer. Then we log to the
console that the space bar was pressed. It’s a nice
way to confirm we are getting the event we expect
when debugging.
After that, we get a reference to our plane game
object and store it in a variable rb. Then we set its
velocity to zero. We do this so that we don’t continue
to multiply our upward velocity with rapid clicking.
This essentially will give the plane little upward
thrusts, without it launching into space. After that
we apply a force using the AddForce method and
multiply the up vector by our strength.
If you save and test this out, you can keep your
plane flying along without falling, as long as you
keep pressing the space bar. You can also use the
strength variable in the component you created to
adjust the strength the upward force push has.


STEP 14: A PHYSICS GROUND
The last step we’ll do before exporting our game for
testing on the web is to add a ground for the plane to
land on when it falls.
Create a new 3D cube game object in your scene
and call it ‘groundSection1’. Add a Box Collider 2D
and Rigidbody 2D to the object like we did with our
plane. Set the Body Type of its Rigidbody 2D to Static
this time. This will ensure the collisions are detected
but the ground will not move when hit. Scale the
cube to fill the screen width at approximately 19.2 x
scale and about 5 z scale.
Now create a new material and assign it your
cube. In the Project Explorer, right-click and select
Create>Material. Call it ‘groundMaterial’. In the
Inspector you can change the colour to green using
the Main Maps Albedo selector, which will open a
colour picker. Once you are happy with your material,
drag and drop it on the ground cube in the scene.
Duplicate your ground object and call it
‘groundSection2’. Create a new empty object in the
scene, call it ‘ground1’ and drag your groundSection1
inside it. Do the same for the other ground object.
Now set the x position of ground1 to -19 like we did
with our sky backgrounds. You should have two
aligned, side by side green ground cubes.
Create another empty game object and drag both
cubes inside it. Call this new object ‘ground’. Now
you have all your ground assets inside one ground
object. Set the y position on this to make the ground
at the bottom of the screen, probably around -4. If
you run the game now, your plane will hit the ground


and not fall off the bottom. It’s not moving yet but
you can apply the same technique we used for the sky
to do that.

STEP 15: EXPORT YOUR SCENE
Let’s export the game and test it in a browser. From
the File menu, pick Build Settings and then select
WebGL as the target platform. Click on Switch
Platform at the bottom of the window. This will load
the required scripts for exporting to WebGL. Click on
Add Open Scenes to add the scene to the Scenes in
the Build window.
Next, click on Player Settings... to open that
Inspector window. Click on the HTML5 icon on the
tabs and set the width to 1920 and the height to 1080.
Also select the Minimal WebGL Template theme. You
can add your own themes to Unity and customise
them but for now use this default one. Press Build,
give the project a name if needed and wait for the
build to finish.

PARTING THOUGHTS
Game and interactive developers traditionally
working in ‘web’ technologies will find it essential
to start exploring tools like Unity. Unity offers
users amazing advantages over traditional WebGL
development pipelines, where real-time previews are
extremely limited.
In the next tutorial in this series, we’ll look at how
to attach obstacles to the ground and to get it moving
at a different rate than the sky to give a sense of
parallax to the scene. We’ll add scoring and a restart
function as well.

Want to make sure you catch Develop a WebGL game with Unity – Part 2 in net
321? Then make sure you subscribe now!

myfavouritemagazines.co.uk/netcb

Free download pdf