2D Gameplay Tutorial Providing 2D Gameplay with Unity 2 Author: Graveck Interactive Last Revision: 19FEB2008 Contents 1. Introduction Why 2D is the Bee's Knees 4 Prerequisites 6 2. Setting The Scene Getting in the 2D MindSet 7 A Closer Look 9 Setting the Level Attributes 10 Object Profile: Level Attributes 12 Object Profile: Death Zone 13 Setting Up the Platforms 13 Object Profile: Platform (Prefabs) 15 Object Profile: PreAssembled Platform 16 Introducing Lerpz, Our Lovely Character 17 Placing Lerpz 18 Directing the Camera 19 Object Profile: Character (Lerpz) 20 Lighting our Scene 22 Spicing up the Character 24 The Camera 26 Object Profile: Main Camera 27 Continuing our Level 28 Script Profile: CameraTargetAttributes 28 Handling Rigidbodies 29 Object Profile: Crate 30 Exercise 31 Moving Platforms 32 Object Profile: Moving Platform 33 The Spaceship 34 Object Profile: Spaceship 35 Well Done! 36 3. Delving Deeper: Scripting Examples Studying the Scripts 38 Moving Platform Particle Effects 38 The Camera Scrolling Script 40 Streamline Your Workflow 41 Spaceship Script Part 1: Defining Helper Classes 43 Spaceship Script Part 2: Controlling the Spaceship 44 Spaceship Script Part 3: Special Effects 47 Finished! 47 4. What's Next? Your Turn 48 Introduction Why 2D is the Bee's Knees It is no secret that Unity can help you create great threedimensional games with ease. あなたがすごい3Dゲームを簡単に作成するのを、Unityが助けることができることは、秘密などではありません。 Its raw power and flexibility allows even beginners to create impressive games without much difficulty. その実力と柔軟性は、初心者でも多くの困難なしに印象的なゲームを作成することができます。 Unity is undoubtedly a great 3D game engine so why on earth would you want to create a 2D game with it? Unityは実にすばらしい3Dゲームエンジンであるのに、あなたはいったいなぜそれで2Dゲームを作成したいのでしょうか? For certain game types, that extra "D" in 3D can be a hindrance. あるゲームタイプにおいて、3Dの余計な「D」は邪魔なのかもしれません。 Some games simply work better with twodimensional gameplay mechanics. いくつかのゲームでは、2Dゲーム動作機能と共に単にうまくいきます。 リストが行き続ける二次元に最もよく抑制されていた状態でうまくいく簡単なパズルゲーム考えがありますが、 結論が多くのゲームが二次元だけの使用の利益を得ることができるということであるとあなたが過去、 またはたぶんした簡単なそれらとおもしろい2D sidescrollingゲームを考えてください。 また、カジュアルなゲームプレーヤーには、Twodimensionalゲームは理解しているのも一般的により簡単です (何かあなたが小さい獲物開発者(「企業としてのカジュアルなゲーム」という記事を見る)であるかどうか考えるものです) Think of those simple and fun 2D sidescrolling games you have played in the past, or maybe you have a simple puzzle game idea that would work best constrained to two dimensions the list keeps going but the bottom line is many games can benefit by the use of only two dimensions. Twodimensional games are also commonly easier for the casual gamer to understand, which is something to consider if you're a small game developer (see the article "Casual Games as a Business"). Luckily, Unity is extremely flexible and can easily handle 2D gameplay! Its world class Ageia PhysX is highly adaptable and can be constrained in many ways including two dimensions. In this tutorial we define a 2D Game as a game where graphics are still in 3D, but restricted motion causes the physics and gameplay only to occur in a single 2D plane. This lets us use 3D models for our content and makes life easier for artists and developers alike. One could say that we're using the best of both worlds: the ease of 2D Sometimes 2D gameplay calls and you can't resist its alluring charm! Unity can handle it. This tutorial shows the steps you need to create a basic 2Dstyle platform game. gameplay and the beauty of 3D graphics with the added attraction that existing developers already have a good 3D asset production pipeline in place. This tutorial will guide you in building a 2D platform game. We have designed this tutorial with beginner / intermediatelevel users in mind; we only ask that you have a baseline knowledge of Unity. We have also included some advanced concepts that may be of interest to more seasoned users. We begin by getting you into the 2D mindset. Then we will walk you through building a 2D world in which your character will move around. After adding a few cool obstacles and scenery, we'll lastly add a rocket ship for the player to fly. We hope you enjoy this tutorial as much as we had fun creating it! The tutorial’s demo level in action. 5 Prerequisites Here are the tools and knowledge you should have before you begin this tutorial: • Unity 2 Although you can benefit from this tutorial if you're a Unity 1.x user, there are some features we use within this tutorial that are only found in Unity 2.0 and above. • Basic Scripting. We assume that you already have a grasp of basic scripting principles. • Familiarity with the Unity user interface. You should also be familiar with Unity’s key interface elements, such as the Inspector, the various Views, and basic draganddrop techniques. If you are unfamiliar with Unity, please take a look at our “Introduction to Unity” videos. You’ll find these in our website’s Resources area. • 3D Modeling Tools (Recommended). Although not required since we supply the models, it is recommended that you have tools to examine the 3D assets. Such tools include Autodesk Maya 8, 3D Studio Max, Cinema 4D, and Cheetah3D. (NOTE: Blender can export FBX files, but cannot currently import this format.) • 2D Graphics Tools (Recommended) Again, it is not required but we recommended that you have tools to examine the 2D assets we supply. Such tools may include Adobe Photoshop, Corel Painter, or one of the budget alternatives such as Pixelmator. Also, be sure to download the project file meant to accompany this tutorial. You’ll find the files in our Resources section: http://unity3d.com/support/resources/ 6 Setting The Scene This section of the tutorial will mostly deal with how to set up GameObjects in the scene view, adding Components, and how to manipulate them in the Inspector. We provide you with a relatively empty scene and your mission is to build onto that scene to make up a more complete level. Bottom line is, we are providing the tools you need and you need to use them to construct a level. Later on in this tutorial we delve a bit deeper and explain how some of the specific scripts work. Getting in the 2D MindSet Now we need to get in the proper mindset and create some common conventions that will remain consistent throughout our project. First we need to define our plane of motion. In other words, we need to restrict motion to only two of the three dimensions, traditionally named X, Y, and Z. To do this we must specifically decide which axis of motion will have no movement. TIP When in the Scene View you can remember which color correlates to which axis with the simple mnemonic device "RGB = XYZ" The usual convention is for the Xaxis to correspond to horizontal movement relative to the observer, while the Yaxis corresponds to vertical movement. The Zaxis therefore corresponds to movements towards and away from the observer for our purposes, this means the Zaxis corresponds to motion towards and away from our camera. Throughout the tutorial let’s keep the following in mind: 7 It’s time to prepare for our journey. In this section we start putting our scene together and look at how Unity can be made to handle 2D games. • Defining our Plane of Motion. We are going to stick with common convention and have our motion be in the XY plane, meaning that no movement will occur in the Zaxis. Vertical movement will be in the Yaxis and horizontal movement will be in the Xaxis. • Restricted Rotation. We also need to restrict our rotation. As a general rule, the only rotation that we will allow is rotation around the Zaxis. Remember the Zaxis passes through the camera; therefore rotation around the Zaxis will result in clockwise and counterclockwise rotation as the Main Camera sees it. There is one exception to this restricted rotation, however. The character is allowed rotation about the yaxis so he can turn from side to side. A 2D game needs just two axes: X and Y. With all this in mind, it's finally time to open our project and scene. If you haven't already done so, please download the project files now. Select File> Open Project and locate the 2D Tutorial project that you have downloaded. Once you have opened the project, find the scene 2D Platformer in the Project Pane and open it. Play the Scene. 8