There has been talk about Unity in shoutbox and in a few posts here and there so I thought I could get the ball rolling on a short and relatively easy tutorial and expand on it if there is interest. Since there seem to be many Sonic fans here I'll try to create a simple Sonic clone as we go. This tutorial requires at least fundamental understanding of programming (variables, functions, classes/objects, instantiations and inheritance) and some basic math/programming concept (especially 2d/3d vectors). I expect you to be able to do some simple Unity things yourself such as install the application and create a new project. You add files to a project by drag/drop to the Project folder.
NOTE: I will sometime be brief in my explanations. Feel free to ask and I will explain and perhaps extend the tutorial text. No question is dumb. Only swedes are dumb.
Requirements (so far):
Unity 5
Green Hill Zone Act 1 png file from https://info.sonicretro.org/Sonic_the_Hedgehog_(16-bit)_level_maps
1 - Create a new project
Start a new project. Add the map file to the project folder.
2 - Create the background
Add a GameObject -> 3D Object -> Quad. This will create a Quad (a square made out of two triangles) within the game world. Make sure its selected and look at the right hand side of the window where it says "Inspector". That is the properties of the selected object. Set its Rotation (under Transform) to 180/0/180. Press F to center screen on object. Might have to fly around in the world to view it at the correct side. Drag and drop image over to the object.
3 - Scale background correctly
Image size is 10240x1280. That is equal to 8:1. Set Transform.Scale to Vector3(8,1,1); As you can see the image now has the correct aspect ratio. It is however too small. Sonics collision model is going to be the equalent to 28 pixels in height. This is measured from one of the chutes he rolls around in. Since we are going to be simulating physics we want to retain real world sizes, so we want sonic to be about 1 meter in height in the game world. 1 meter is the same as 1 unit inside of Unity. In other words we want 28 pixels to be the same as Unity 1 unit. So we take the height of the image 1280 pixels and divide by 28 to find its height ratio. Answer is 45.7(...) so we set the image Transform.Scale.Y to to 45.7. Since we know that the X scale is 8 times longer I just hit * 8 on my calculator and we get 365.7(...). Input this value in Transform.Scale.X. You now get a nice, big and blurry image inside Unity. Don't worry about it being blurry for now! I have added a 1x1x1 unit cube at one of the chutes to see that we now have the correct size background for a 1 unit high Sonic model.
4 - Set background properties
Select the Quad in the Hierarchy. Notice in the inspector that there is a checkbox next to "Mesh Collider". Remove it so that it is in off position. Now select the image in your project files. Set texture type to "Editor GUI and Legacy GUI", Filter Mode to Point (no filter), Max Size at 8192 and Format at 16 bits. Hit apply. This is temporary for now and will make the image look better inside the editor.
5 - Move and set up camera
Time to set the camera up correctly. Select "Main Camera" in the Hierarchy and change its Transform.Position to Vector3(175,-9,10); and its Transform.Rotation to Vector3(0,180,0); Further down under Camera set its Clear Flags to Solid Color, its Background to Hex color 0088EEFF (which I found in Photoshop to be the background color in the map) and most importantly set its Projection to be Orthographic. Above your 3d view click the Game tab to get a look at what we have so far.
6 - Change ambient lighting
The solid color on the background to the left of the game image is how we want the background in the image to look like. Right now that isn't happening because everything in the game world is being shaded and lit by some default settings. First thing we do is select the Direction Light in the Hierarchy and delete it. Now there is no light source in the game world, there is just the default light settings (ambient coloring). Go to Window -> Lighting and set Ambient Source to be Color. Click the color-picker underneath and set it all the way to white. If you look at your image this is as close to the same color you get after Unity is done processing the image.
7 - Create temporary platform
Go from Game view back to Scene view. Select GameObject -> 3d Object -> Cube. Position it at Transform.Position(180,-12.1,0); to position it just under Sonics feet. Remember the F command to center camera on the selected object. Now lets make this a bit wider by settings its Transform.Scale.X to 10. This block will serve as a platform to walk on when testing.
8 - Create character
Now we want to create our character. For that we will use a capsule as collision shape. When Sonic rolls into a ball it is better to change shape into a ball instead. Whatever the case, set the Transform.Position of the newly created Capsule to Vector3(180,-10.8,0); and its Transform.Scale to Vector3(0.8,0.71,0.8); There are many ways to move an object around in a game world. Most of the time you don't want your character to be moved around by the physics simulation (by applying forces to the object) but rather to be manipulated directly. In Sonics case however his movement (especially as a ball) is highly dependent on physics and so it would be beneficial to just push the guy around the level using the built-in physics system in Unity. To make the capsule behave like a physical object make sure that the Capsule is still selected, then click on the Add Component button in the Inspector. Select Physics -> Rigidbody. The Rigidbody class adds the correct physical behavior of the object inside the game world. When we want to manipulate and control the object we don't control its Transform directly but rather apply forces to its Rigidbody class. Anyways, click on the Game tab again and let's try to run this game for the first time. Press the Play button and see your game in action. The capsule should first fall to the block (since it is positioned slightly above it) and then slowly tilt to the side and fall off.
9 - Set movement and rotation constraints
We don't want the capsule to rotate at all and instead stay fixed. Luckily we can control that in the Rigidybody. Click on the little arrow next to Constraints and select all three Freeze Rotation boxes. Since we also don't want our character to move in the Z direction (towards/away from the screen) we also select the Z Freeze Position box.
10 - Create script and set development environment
We have now set up the game world about as much as we need before we start coding. Just a word about that though; the default editor for Unity seems to have been switched from Mono to Visual Studio. While I use Visual Studio for non-Unity related projects I for some reason prefer Mono when working with Unity. If you want to use the same then go to Edit -> Preferences -> External Tools -> External Script Editor: MonoDevelopt (built-in). Whatever the case, go the the Projects folder (where the level image is) and right-click and select Create -> C# Script. NOW WAIT A SECOND! Before you press enter on naming the C# just make sure you don't change your mind about the file name. When you press enter there will be information inside the script that refers to the filename. Should you later change the filename you will get an error. I named by script CharacterController. Now double-click on it to open it in your code editor.
edits: Changed #2 for clarity.
NOTE: I will sometime be brief in my explanations. Feel free to ask and I will explain and perhaps extend the tutorial text. No question is dumb. Only swedes are dumb.
Requirements (so far):
Unity 5
Green Hill Zone Act 1 png file from https://info.sonicretro.org/Sonic_the_Hedgehog_(16-bit)_level_maps
1 - Create a new project
Start a new project. Add the map file to the project folder.
2 - Create the background
Add a GameObject -> 3D Object -> Quad. This will create a Quad (a square made out of two triangles) within the game world. Make sure its selected and look at the right hand side of the window where it says "Inspector". That is the properties of the selected object. Set its Rotation (under Transform) to 180/0/180. Press F to center screen on object. Might have to fly around in the world to view it at the correct side. Drag and drop image over to the object.
3 - Scale background correctly
Image size is 10240x1280. That is equal to 8:1. Set Transform.Scale to Vector3(8,1,1); As you can see the image now has the correct aspect ratio. It is however too small. Sonics collision model is going to be the equalent to 28 pixels in height. This is measured from one of the chutes he rolls around in. Since we are going to be simulating physics we want to retain real world sizes, so we want sonic to be about 1 meter in height in the game world. 1 meter is the same as 1 unit inside of Unity. In other words we want 28 pixels to be the same as Unity 1 unit. So we take the height of the image 1280 pixels and divide by 28 to find its height ratio. Answer is 45.7(...) so we set the image Transform.Scale.Y to to 45.7. Since we know that the X scale is 8 times longer I just hit * 8 on my calculator and we get 365.7(...). Input this value in Transform.Scale.X. You now get a nice, big and blurry image inside Unity. Don't worry about it being blurry for now! I have added a 1x1x1 unit cube at one of the chutes to see that we now have the correct size background for a 1 unit high Sonic model.
4 - Set background properties
Select the Quad in the Hierarchy. Notice in the inspector that there is a checkbox next to "Mesh Collider". Remove it so that it is in off position. Now select the image in your project files. Set texture type to "Editor GUI and Legacy GUI", Filter Mode to Point (no filter), Max Size at 8192 and Format at 16 bits. Hit apply. This is temporary for now and will make the image look better inside the editor.
5 - Move and set up camera
Time to set the camera up correctly. Select "Main Camera" in the Hierarchy and change its Transform.Position to Vector3(175,-9,10); and its Transform.Rotation to Vector3(0,180,0); Further down under Camera set its Clear Flags to Solid Color, its Background to Hex color 0088EEFF (which I found in Photoshop to be the background color in the map) and most importantly set its Projection to be Orthographic. Above your 3d view click the Game tab to get a look at what we have so far.
6 - Change ambient lighting
The solid color on the background to the left of the game image is how we want the background in the image to look like. Right now that isn't happening because everything in the game world is being shaded and lit by some default settings. First thing we do is select the Direction Light in the Hierarchy and delete it. Now there is no light source in the game world, there is just the default light settings (ambient coloring). Go to Window -> Lighting and set Ambient Source to be Color. Click the color-picker underneath and set it all the way to white. If you look at your image this is as close to the same color you get after Unity is done processing the image.
7 - Create temporary platform
Go from Game view back to Scene view. Select GameObject -> 3d Object -> Cube. Position it at Transform.Position(180,-12.1,0); to position it just under Sonics feet. Remember the F command to center camera on the selected object. Now lets make this a bit wider by settings its Transform.Scale.X to 10. This block will serve as a platform to walk on when testing.
8 - Create character
Now we want to create our character. For that we will use a capsule as collision shape. When Sonic rolls into a ball it is better to change shape into a ball instead. Whatever the case, set the Transform.Position of the newly created Capsule to Vector3(180,-10.8,0); and its Transform.Scale to Vector3(0.8,0.71,0.8); There are many ways to move an object around in a game world. Most of the time you don't want your character to be moved around by the physics simulation (by applying forces to the object) but rather to be manipulated directly. In Sonics case however his movement (especially as a ball) is highly dependent on physics and so it would be beneficial to just push the guy around the level using the built-in physics system in Unity. To make the capsule behave like a physical object make sure that the Capsule is still selected, then click on the Add Component button in the Inspector. Select Physics -> Rigidbody. The Rigidbody class adds the correct physical behavior of the object inside the game world. When we want to manipulate and control the object we don't control its Transform directly but rather apply forces to its Rigidbody class. Anyways, click on the Game tab again and let's try to run this game for the first time. Press the Play button and see your game in action. The capsule should first fall to the block (since it is positioned slightly above it) and then slowly tilt to the side and fall off.
9 - Set movement and rotation constraints
We don't want the capsule to rotate at all and instead stay fixed. Luckily we can control that in the Rigidybody. Click on the little arrow next to Constraints and select all three Freeze Rotation boxes. Since we also don't want our character to move in the Z direction (towards/away from the screen) we also select the Z Freeze Position box.
10 - Create script and set development environment
We have now set up the game world about as much as we need before we start coding. Just a word about that though; the default editor for Unity seems to have been switched from Mono to Visual Studio. While I use Visual Studio for non-Unity related projects I for some reason prefer Mono when working with Unity. If you want to use the same then go to Edit -> Preferences -> External Tools -> External Script Editor: MonoDevelopt (built-in). Whatever the case, go the the Projects folder (where the level image is) and right-click and select Create -> C# Script. NOW WAIT A SECOND! Before you press enter on naming the C# just make sure you don't change your mind about the file name. When you press enter there will be information inside the script that refers to the filename. Should you later change the filename you will get an error. I named by script CharacterController. Now double-click on it to open it in your code editor.
edits: Changed #2 for clarity.

