This recipe presents the steps to create the 2DSpaceGirl mini-game, on which all the recipes of this chapter are based.
For this recipe, we have prepared the images you need in a folder named Sprites
in the 1362_02_01
folder. We have also provided the completed game as a Unity package in this folder named Simple2DGame_SpaceGirl
.
To create the simple 2D mini-game Space Girl follow these steps:
Sprites
into your project.Default is Full Screen,
and set the width to 800
and height to 600
, as shown in the following screenshot:background-blue
from the Project panel (folder Sprites
) into either the Game or Hierarchy panel to create a GameObject for the current scene.background-blue
to Background (in the Sprite Renderer component).star
from the Project panel (folder Sprites
) into either the Game or Hierarchy panel to create a GameObject for the current scene.star
in the Hierarchy scene.star
to Foreground (in the Sprite Renderer component).star
a Box Collider 2D (Add Component | Physics 2D | Box Collider 2D) and check its Is Trigger
, as shown in the following screenshot:girl1
from the Project panel (folder Sprites
) into either the Game or Hierarchy panel to create a GameObject for the player's character in the current scene. Rename this GameObject player-SpaceGirl
.player-SpaceGirl
to Foreground (in the Sprite Renderer component).player-SpaceGirl
a Box Collider 2D (Add Component | Physics 2D | Box Collider 2D).player-SpaceGirl
a RigidBody 2D (Add Component | Physics 2D | Rigid Body 2D). Set its Gravity Scale to zero (so it isn't falling down the screen due to simulated gravity), as shown in the following screenshot:Scripts
.PlayerMove
(in folder Scripts
) and add an instance as a component to GameObject player-SpaceGirl
in the Hierarchy:using UnityEngine; using System.Collections; public class PlayerMove : MonoBehaviour { public float speed = 10; private Rigidbody2D rigidBody2D; void Awake(){ rigidBody2D = GetComponent<Rigidbody2D>(); } void FixedUpdate(){ float xMove = Input.GetAxis("Horizontal"); float yMove = Input.GetAxis("Vertical"); float xSpeed = xMove * speed; float ySpeed = yMove * speed; Vector2 newVelocity = new Vector2(xSpeed, ySpeed); rigidBody2D.velocity = newVelocity; } }
Scenes
).You have created a player character in the scene, with its movement scripted component PlayerMove
. You have also created a star GameObject (a pickup), tagged Star
and with a 2D box collider that will trigger a collision when the player's character hits it. When you run the game, the player-SpaceGirl
character should move around using the W A S D, arrow keys, or joystick. Currently, nothing will happen if the player-SpaceGirl
character hits a star since that has yet to be scripted.
You have added a background (GameObject background-blue
) to the scene, which will be behind everything since it is in the rearmost sorting layer Background. Items you want to appear in front of this background (the player's character and the star so far) are placed on sorting layer Foreground. Learn more about Unity tags and layers at http://docs.unity3d.com/Manual/class-TagManager.html.
3.135.182.221