- 最后登录
- 2021-9-15
- 注册时间
- 2011-7-15
- 阅读权限
- 100
- 积分
- 50625
- 纳金币
- 53202
- 精华
- 32
|
This is part 4 of our tutorial series on making a 2D game in unity3d with freely available tools and plugins. Part 1introduced you to the tools we’re using, in Part 2 we built our first level and in Part 3 we hooked up the control scripts and setup the ladders and ropes so that the player can navigate levels. In this article we’re going to create a pickup item, hook up the scoring system and add the interface text which reports score, lives and level.
Hopefully you already went through part 2 and part 3 of the series, if you haven’t already you might want to go back and do them now so everything in this part will make sense.
If you’d rather skip ahead, you can download the project up to this point. You can alsoclick here to play the game as it will be at the end of Part 4.
Adding The Scoring and Pickup Scripts
The game is working great so far – the player can move, climb ladders and shimmy across ropes. Now we’re going to add a “gold chest” pickup item and an interface to track scoring, player lives and the current level.
Download scoring and pickup scripts and unzip the file somewhere on your hard drive.
Copy
Scoring.cs
and
Pickup.cs
from the extracted .zip file and paste them into your project’s
Assets/Scripts
folder.
Modifications To Existing Scripts:
You’re going to need to make a few changes to the scripts from part 3 so that these new scripts will work.
Open xa.cs script and then uncomment the first line in the Class:
public static Scoring sc;
In the Start function, uncomment this line:
sc = (Scoring)(this.gameObject.GetComponent("Scoring"));
Open Player.cs, find the OnTriggerEnter function (line 256) and then uncomment the following code block:
if (other.gameObject.CompareTag("ickup"))
{
if (other.GetComponent<ickup>())
{
other.GetComponent<ickup>().PickMeUp();
xa.sc.Pickup();
}
}
Making The Pickup Sprite
You might remember from part 2, that you already added the
pickup.png
to the
level
sprite atlas and then created a
level
container object that points to the sprite atlas so we don’t need to do any additional Container setup to create the pickup.
Drag & drop the Sprite object from
Orthello –> Objects –> Sprites
into the Hierarchy or Scene which will create a new object named something like “Sprite (id=-3700)“. Rename that object to “
pickup
“.
Drag the level object that we created in part 2 from
OT –> Containers
and drop it on to the
Sprite Container
slot in the Inspector.
Your sprite will appear but it looks like the brick sprite that we made before, that’s because the brick is the first texture on the Sprite Atlases index.
Click and hold your mouse over the word “
Frame Index
” in the inspector (with the pickup object selected) and then drag the mouse to the right to scroll through the textures on the sprite atlas. The pickup texture is at index
16
so set it to that. The sprite should now look like a a white rectangle with a red box in the center and a black background.
Adjusting The Collision:
The sprite is a square, but we only want the object to be picked up only when the player touches the white part of the pickup.
Check the
Collidable
checkbox to add collision to the object.
Open up the drop down list next to
Physics
and then select
Custom
.
Under
Box Collider
, set the
Center Y
to
-0.15
,
Size X
to
0.8
and
Size Y
to
0.5
. Leave
Size Z
set to
0.4
.
Setting The Tag:
We need to tag the pickup so that the player will trigger it when touched.
Create a new tag by going to
Edit –> Project Settings –> Tags
Add a new
Tag
in the Tags list (probably in Element 3 if you’ve been following the series) named “
Pickup
” (without the quotes).
It’s important that the name is correct since Player.cs looks for this exact name.
Select the pickup object in the Hierarchy, then click on the drop down list next to
Tags
at the top of the Inspector and then select
Pickup
.
Add The Script and Make It A Prefab
Now we just need to add the
Pickup.cs
script to the sprite so that it will get “picked up” when the player touches it and we also need to turn it into a prefab so that it’s easy to place in all your levels.
Drag the
Pickup.cs
script from the Project Scripts folder and drop it on to the
pickup
sprite in the Hierarchy.
Drag the
pickup
object from the Hierarchy into the
Prefabs
folder in the Project tab to create prefab from the object.
The pickup is designed so that you can snap the bottom edge (the black part) of the sprite to the top edge of a brick sprite the white part of the powerup will be the correct height from the top of the brick. Duplicate your new pickup prefab a few times and place it around you level.
If you followed all of the steps so far, your pickup should look like this:
Add The Interface Text
At this point if you place the pickup in your level so that the player can pick it up, the sprite will disappear correctly as if he picked it up, but you’ll get some errors in the console because the Scoring script isn’t in the scene yet and we don’t have any UI to report the score. So let’s fix that.
First add the
Scoring.cs
script to the scene: Drag
Scoring.cs
from your Project’s Scripts folder and drop it on to the
Scripts
object in the Hierarchy. If you select the
Scripts
object, you’ll see that the
Scoring
script has a bunch of slots for text objects that we need to add – but first we need to create the text objects.
Download the G7 Silkworm font and unzip the file somewhere on your computer. This font isn’t exactly the same as the one used in the original Lode Runner game, but it’s pretty close. If there’s another font you’d rather use, go for it. And if you find a font that’s closer to the Lode Runner font, please let me know in the comments.
Create a new folder in your
Assets
folder named
Fonts
, locate the
silkworm.TTF
font on your hard drive and then copy it into the
Assets/Fonts
folder.
In Unity, select the
silkworm
font in your Project’s Fonts folder and then in the Inspector set the
Font Size
to
26
.
Adding The Score Text:
Drag the
silkworm
font from the
Fonts
folder in the Project panel and drop it into the Hierarchy. This will automatically create a new
GUI Text
object in the Hierarchy using the
silkworm
font.
Rename the object to “
Score Label
“
In the
Text
field under GUIText in the Inspector, type “
SCORE
” in all caps without the quotes. The Silkworm font only has uppercase letters so don’t try to use lowercase letters.
When you add a font to the scene, the X and Y position are automatically set to X 0.5 and Y 0.5, but we’re going to set the position of our fonts using the Pixel Offset so reset the
X
and
Y Position
to
0
.
Change
Pixel Offset X
to
16
and
Pixel Offset Y
to
592
. This should position the
SCORE
text to the upper left hand corner of the Game view (note you won’t see the text in the Scene view so look at the Game view to help position the text).
We also want to keep the scene nicely organized and make it a little easier to hook up the interface objects to the Scoring script later on. You’re going to make the Score Label object a child of the Scripts object and then you’ll duplicate each of the other text objects from this one in order to save a few of the setup steps.
Select the
Score Label
object in the Hierarchy and then drag and drop it onto the
Scripts
object in the Hierarchy so that it becomes a child of the Scripts object.
Expand the little arrow next to the
Scripts
object, select
Score Label
object and then duplicate it.
Rename the new text object to “
Score Value
“.
Change the
Text
to
0000000
(7 zeroes).
Change the
Pixel Offset X
to
150
and leave the
Y
set to
592
.
Adding The Lives Text:
Select either the Score Label or the Score Value text object and duplicate it.
Rename the new text object to “
Lives Label
“
Change the Text to
LIVES
.
Change the
Pixel Offset X
to
348
and leave
Y
set to
592
.
Duplicate
Lives Label
object and rename it to “
Lives Value
“.
Change the
Text
to
000
(3 zeroes).
Change the
Pixel Offset X
to
480
and leave
Y
set to
592
.
Adding The Level Text:
Select one of the other text objects you already created and duplicate it.
Rename the new text object to “
Level Label
“
Change the
Text
to
LEVEL
.
Change the
Pixel Offset X
to
576
and leave
Y
set to
592
.
Duplicate
Level Label
and rename the new object to “
Level Value
“
Change the
Text
to
000
(3 zeroes).
Change the
Pixel Offset X
to
710
and leave
Y
set to
592
.
Hooking Up The Scripts:
Now we need to connect the interface text objects to the Scoring script.
Select the
Scripts
object in the Hierarchy. Under the Scoring script in the Inspector, you’ll see slots for each of the GUIText objects we just created.
Select each of the text objects from the Hierarchy and drag and drop them into the corresponding slot on the Scoring script. For example, drag
Level Label
and drop it on to the
Level Label Text
field.
Once all of the text objects are connected to the
Scoring
script, press
Play
in Unity. You should see the SCORE, LIVES and LEVEL text turn to a red color that matches the border color and the number of lives should change to 005 and the level number should change to 001.
Now if you run the character over a pickup object in the game, the Score should increase by 250.
Make The Top Border
To help keep the interface text separate from the gameplay area, we’re going to add a thin border just under the interface text.
Create a new cube by going to
Game Object –> Create Other –> Cube
Rename the object to “
border top
“.
Set the
Transform Position X
to
0
,
Y
to
8.7
and
Z
to
1
.
Set the
Scale X
to
26
,
Y
to
0.2
and
Z
to
1
.
Find the
border
material in the Materials folder and then drag and drop it on to the
border top
object. We created the border material in a previous article.
If you followed all of the steps above, then your Hierarchy and Inspector (with the Scripts object selected) should look the following:
Creating The GLOBAL Prefab
Next we want to do some things to keep the scene nicely organized and also to make it easy to add the key components to any new levels that you create. We’re going to create a GLOBAL prefab that contains the Main Camera, the Scripts (which includes the interface text objects) and the top and bottom borders.
Create an empty game object by boing to
Object –> Create Empty
, zero out the transforms so that it’s at 0,0,0 on the X,Y,Z and then rename the object to “
GLOBAL
“.
Select the
Main Camera
in the Hierarchy and then drag and drop it onto the
GLOBAL
object so that the Main Camera becomes a child of GLOBAL.
Select the
Scripts
object in the Hierarchy and drag and drop it onto the
GLOBAL
object.
Select the
border bottom
and
border top
objects (make sure they’re not already a child of some other game object first) and drag and drop them onto the
GLOBAL
object.
That’s everything we need for the GLOBAL object, so now let’s turn it into a prefab that can be easily added to other levels and updated later if we need to make changes.
Select the
GLOBAL
object in the Hierarchy and then drag and drop it onto the
Prefabs
folder in the Project panel.
Creating The Player Prefab
In Part 3 of the series, I forgot to create a prefab from our player character, so let’s do that now if you haven’t already.
Select the
player
object in the Hierarchy and zero his Transform position X to 0, Y to 0 and leave the Z position set to -1. This step is optional, I just like to have my game objects centered when I initially create a prefab.
If you’ve followed all of the steps so far, then your Project and scene Hierarchy should look like the following image:
Conclusion
We’re getting a lot closer to having a complete game. Over the course of the next few tutorials I hope to add enemy AI (still working on a good solution for them), breakable bricks, a front end interface, an exit ladder (for exiting levels), some kind of manager for keeping track of your current level and a few other things like a game over screen. Let me know in the comments if there are other things you’d like to see covered.
If you followed all of the steps so far, then your game should look something like this image (click to see a larger version):
You can download the project up to this point and you can play the web version of the project here.
If you like this post, please be sure to say hi in the comments and follow me on Twitterand Facebook. Your support helps to keep these tutorial coming. This blog post is part of iDevBlogADay, a collection of indie developers writing about their development experiences.
转自:http://www.rocket5studios.com
|
|