p2 - Shooter Part 1
p2 - Shooter Part 1
Adding a background
Your first background will be static. We will use the
following image:
(Right click to save the image)
What is a sprite?
Some readers have reported that, in their project, this dialog was empty.
The reason is that for some Unity installations, even with a fresh new 2D
project, images are imported as “Texture” and not “Sprite”.
To fix this, you need to select the image in the “Project” pane, and in the
“Inspector”, change the “Texture Type” property to “Sprite”:
We don’t know why everybody doesn’t seem to have the same behavior.
This means that all images are at the same depth, ie. 0.
And you (as well as the graphics engine) don’t really
know who’s going to be displayed first.
Let’s add some layers to fit our needs (use the “+”
button):
Apply the Background layer to our background sprite:
Unity will find the objects inside the image and will slice them
automatically. You can specify the default pivot point, or set a minimum
size for a slice. For a simple image without artifacts, it’s really efficient.
However, if you use this tool, be careful and check the result to be sure to
get what you want.
Now, under the image file, you should see the two
sprites separately:
Adding them to the scene
Prefabs
Note on the “Prefab” buttons: if you modify the game object later, you
can “Apply” its changes to the Prefab or “Revert” it to
the Prefab properties (canceling any change you’ve made on the game
object). The “Select” button move your selection directly to
the Prefab asset in the “Project” view (it will be highlighted).
Creating prefabs with the platform objects will make
them easier to reuse later. Simply drag the Prefab into
the scene to add a copy. Try to add another platform
that way.
Add a sprite
If you have any trouble, refer to the previous part. We did exactly the
same procedure for the background and props.
You can see the collider in the editor “Scene” view and
tweak its size in the “Inspector” with the “Size”
property.
Tip: There is another way to edit a box collider. Select a game object
with a box collider and enable the “Edit Collider” toggle in the component.
You can observe that the box collider (the green rectangle) is now
showing four small handles onto. Drag one of them to change the shape
of the box.
It’s way too large for a real shmup but it’s still smaller
than the sprite:
We also recommend to use this menu the first time you have to script,
because Unity will create the solutions and link the Unity libraries in them
(for Visual Studio, Xamarin Studio or MonoDevelop).
If you simply open the script instead, the compiler of your IDE will likely
catch some errors because it won’t know Unity.
It doesn’t matter because you will never compile directly with it, but it is
nice to have the autocompletion on the Unity objects and a first pass on
errors.
By using them, you are adopting the new physics engine integrated in
Unity 4.3 for 2D games (based on Box2D) instead of the one for 3D
games (PhysX). The two engines are sharing similar concepts and
objects, but they don’t work exactly the same. If you start to work with
one (favor Box2D for 2D games), stick to it. This is why we use all the
objects or methods with a “2D” suffix.
void Update()
{
// 3 - Retrieve axis information
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
void FixedUpdate()
{
// 5 - Get the component and store the reference
if (rigidbodyComponent == null) rigidbodyComponent =
GetComponent<Rigidbody2D>();
Thanks to your feedback, we updated the scripts to help people learn the
good practices of game object movement.
Be careful: modifications when the game is executed (or played) are lost
when you stop it! It’s a great tool for tweaking the gameplay, but
remember what you are doing if you want to keep the changes.
However, this effect is also handy: you can destroy your game completely
during the execution to test something new, without being afraid of
breaking your real project.
This was the first sign of life in our game! Let’s add
more!
Sprite
/// <summary>
/// Simply moves the current game object
/// </summary>
public class MoveScript : MonoBehaviour
{
// 1 - Designer variables
/// <summary>
/// Object speed
/// </summary>
public Vector2 speed = new Vector2(10, 10);
/// <summary>
/// Moving direction
/// </summary>
public Vector2 direction = new Vector2(-1, 0);
void Update()
{
// 2 - Movement
movement = new Vector2(
speed.x * direction.x,
speed.y * direction.y);
}
void FixedUpdate()
{
if (rigidbodyComponent == null) rigidbodyComponent =
GetComponent<Rigidbody2D>();