You are on page 1of 15

Learning Unity3D using C#

Outstanding Scholars Work Philip Keefner


103629078

Fall 2012

Learning Unity Using C#

Table of Contents
Summaries C# Tutorials Unity Basic Video Tutorials 2D Gameplay Tutorial Unity C# Tutorials Mario Personal Project Conclusion 3 3 3 4 5 5

Created Reference Sections 1) Unity Basics 2) C# in Unity 6 11

Outside References

15

Learning Unity Using C#

C# Tutorials I began by taking a look at various C# tutorials. I read through a couple and found out that C# is very similar to Java and that the syntax and expressions are very similar, if not the same, as those in Java. I began to realize that starting so far down was not going to help me and that I should turn my attention to the Unity game engine and then return to C# when I found out more about how it is used in Unity and what specifically I would need to know about it.

Unity Basic Video Tutorials I figured that the best place to start with Unity would be to watch the video tutorials suggested by the welcome window with the first start-up after downloading the engine itself. There were six videos that I went through which teaches the basics of navigating through the engine and some very simple but useful actions a user can do when working with the engine. What I Learned Through these tutorials I learned how to navigate the Unity interface effectively and how to use the game engine as demonstrated by the Reference Section 1) Unity Basics.

2D Gameplay Tutorial After learning about the basics of unity, I was interested in starting right into a game. From the Unity3D website I found a tutorial for a 2D platformer game. Personally, I like platformer games and I thought that constraining unity to two dimensions would be a natural place to start and help me ease into learning to create 3D games. I downloaded the PDF and project components for the tutorial then read and followed through the whole thing. What I Learned Through the tutorial I reinforced a lot of what I had learned through the videos by examples as well as getting into some not so simple scripts. The scripts included were in JavaScript but I easily found C# translations of them on the internet. This tutorial also taught me more about how much is involved in creating games and different small sections which game some understanding of controllers and colliders and those kinds of parts of the game.

Learning Unity Using C#

Continued At the end of the tutorial it suggested making changes to the game in order to test yourself. One such change I had already thought of while following along. I decided to change the GUI button interface for switching between controlling the character and the spaceship within the level to switching by simply pressing a button. Then I went on to restrict switching control to only when the character is close to the ship, and finally, making the character disappear when controlling the ship and reappear when switching control back to the character, wherever the ship is. I have included the executable of these changes in an attachment entitled MyFirstLevel. The Controls are in a text document also attached entitled MyFirstLevelControls. I have also attached a copy of the CameraFocus.cs script where I added all of the code for switching characters.

Unity C# Tutorials After completing the 2D Gameplay Tutorial, I still felt that I did not have a full understanding of basic scripting elements in C# for Unity, so I sought out another tutorial to help me with this. This was my favourite set of tutorials because it managed to stay simple enough to learn everything line by line from scratch, as well as delve into some more complicated scripts all the while teaching you about the different specific basic elements in a C# script used in unity. What I Learned I learned a lot about different elements in C# that are used in Unity scripts. These tutorials were exactly what I wanted to learn about in order to get a basic understanding of C# scripts in Unity. After completing the tutorials, I thought of the idea of making a reference sheet for all that I had learned so that I could have easy access to look up of functions and other elements without having to search them out separately online. This was the inspiration for Reference Sheet 2) C# in Unity. Continued I also have attached an executable for the Cube Run Game in the last tutorial that I did which is pretty much the same as what was directed by the tutorial except for the colours. One thing I did add to it though was a button to fall faster down to a platform which made the game easier to land on the platforms and more fun in my opinion.

Learning Unity Using C#

Mario Personal Project After completing the other tutorials I wanted to test out how much I had learned. So I came up with the idea of trying to recreate the first level from the original Mario game for the NES. Not worrying about graphics, I altered the skin for the character in the 2D platformer tutorial and left the shape alone, as well as collected sprites from the Mario game in order to use for blocks and other things. Borrowing objects and the character with its controls from the 2D platformer tutorial, I created a new scene and built it to mimic the level in mind then started to mimic the different components from the game. Currently, I only have the first mushroom block with the mushroom and Marios growth upon picking up the power-up, but I am continuing to work on it still. I have attached a build of the level so far entitled MarioLevel.

Conclusion In conclusion I think that I have learned a lot about the Unity 3D game engine and its scripts using C# over the past semester. After school work, studying, and working on the weekends, most of my time has gone in to working with Unity. I feel confident about what I have learned and hope that it is what was expected from me. If it is not then I am truly sorry, and will most certainly learn from my mistakes if given the chance to continue in the outstanding scholars program. I requested to work with Dr. Goodwin because I have a passion for creating videogames and am hoping to make this field of work my career. I will continue to work with and learn more about Unity as it is a great game engine to work with which I have found out through this semester. If nothing else I am extremely grateful for the opportunity to have been directed towards this for learning. If I appeared unambitious or ungrateful I apologize but this is not what I was trying to do. I will make sure that nothing like this happens again. Thank you for taking the time to read my report.

Learning Unity Using C#

Unity Basics
Unity Layout The unity layout for the different windows can be configured by the drop down menu in the top right corner which has the current layout name in it. The layouts are: 2 by 3 4 Split Tall Wide

You can make your own custom layouts as well if you wish and save them in the same drop down menu.

Scene Window The scene window shows you how your scene is physically set up with any of the objects and gizmos placed inside of it. Navigation inside of the scene window can be achieved in various ways. Arrow Keys- move through the scene in the direction pressed Right Mouse Button - rotates the view in the window Right Mouse Button while holding Alt- zooms the view in the window Middle Mouse Button- pans the view in the window Mouse Wheel- zooms the view in the window

You can also click the Hand symbol in the toolbar at the top left of the program to add some more controls for navigating the scene. Left Mouse Button- pans the view in the window Left Mouse Button while holding Alt- rotates the view in the window

Learning Unity Using C#

You can snap the view to one of the axis by clicking one in the image in the top right corner of the scene window. Also, you can press the cube in the center of the axes for an indirect view of all of the axes. You can manipulate the game objects in your scene in this window as well. When a game object is highlighted you can move it freely by left clicking and dragging in the direction you want to move it, or to be more precise, you can choose one of the color coordinated axes arrows to move it along that axes only. Using the three options in the top left on the toolbar you can Move an object this way, Rotate an object this way, or Scale an object this way.

Game Window The game window shows you what is currently being seen by the main camera in your game. The main camera is an object which can be seen and moved within the scene window, it is not the view in the scene window itself. The game window is in a tab with the scene window in most layouts so you only see one or the other depending on which tab you click. The tabs are located at the top of the scene window or game window. You can test your game by clicking the Play button in the top middle of the toolbar. This will automatically switch the game window to the front. While testing your game in this way, you can also use the Pause button to pause and un-pause the game, as well as the Step button to make the game step forward one frame at a time. All three of these buttons are located next to one another. NOTE: if you switch back to the scene window and make any changes while the game is playing, or make any changes in any of the other windows, they will revert back to how it was before playing the game when the game stops.

Hierarchy Window The hierarchy window is used to keep track of all of the objects currently in the scene. It appears as a list of objects and if one is selected, it is also selected within the scene. If you double click an object, the scene view changes so it is focused on that object. You can rename objects in the hierarchy to keep track of them as well as parent objects with other objects. Parenting an object with another object means that if you move the parent object within the scene, the children of that object move with respect to the parent as well.
7

Learning Unity Using C#

This is very useful to keep several objects that make up one larger object always together. Parenting an object with another object is achieved by simply clicking the child object in the hierarchy window and dragging it onto the parent object, still in the hierarchy window. When an object is a parent with children, it has an arrow head beside it that is either open (pointing down) or closed (pointing to the right). When open, it shows all of the parents children below it in the list but indented. When closed, the children do not appear in the hierarchy windows list of objects. NOTE: when you have a parent object and any number of child objects, you cannot move the parent object without its children. A useful trick that I learned in order to keep objects organized is to create an empty object to use as a parent for child objects if there is no base object that always moves with all of its children.

Project Window The project window shows all of the assets that are in the folder associated with the specific project you are working on. Assets can include textures, materials, physic materials, scripts, prefab objects, and more, just to name some common ones I have worked with so far, as well as folders to organize them. Brief descriptions of these assets are: Texture- a flat image that is included in materials in order to render an object visually in a scene in more than just a colour. Material- has a texture or sometimes multiple textures associated with it which it wraps around an objects mesh in order to render it visually in a scene in more than just a colour. NOTE: if you change the texture in a material for one object, it changes for all objects with the same material. If you wish to change a texture for one instance of an object but not all, change the material for the object. Physic Material- used in Rigidbodies, the physical aspects of objects with properties affected by the physics system built in to unity. An example of a property of physic materials is friction. Scripts- programs that are attached to objects and execute within the game. In unity, they are written in JavaScript, C#, or Boo. Prefab- an object with certain properties that are saved in order to be used more than once. Any object can be made a prefab by simply clicking and dragging it from the hierarchy window into the project window. If a parent is made a prefab, all of its
8

Learning Unity Using C#

children are included in the prefab as well. A prefab can be added into the scene by clicking and dragging from the project window to either the hierarchy window or the scene window. Inspector Window The inspector window is used to manipulate all properties of any selected object. The inspector can be used to manipulate prefabs or specific game objects within a scene. If an object in a scene is changed which has a prefab, the prefab can be updated by clicking the Apply button near the top right of the inspector. A component can be added to an object either via the dropdown menu at the top of the program or by clicking and dragging from the project window to the inspector window with the target object already selected. The inspector can be used to manipulate the properties of pretty much everything about the object. The most common properties which are always included and always located at the top of the inspector are the objects transform properties. The transform properties are the physical location of the object within the scene. They include the position of the object, the rotation of the object, and the scale of the object, all with x, y, and z axis numerical components. All three of those properties can be manipulated in the inspector in order to precisely manipulate the object within the scene.

Toolbar The toolbar at the top has already been discussed but to recap, the important buttons for now, from left to right, are: Hand- to add more maneuverability to the scene view Move- to move an objects position within the scene Rotate- to rotate an object within the scene Scale- to scale an object within the scene Play- to run the game for testing Pause- to pause and un-pause the game when it is running Step Forward- to move one frame forward within the game while it is paused Layout Drop Down Menu- to select the layout

Learning Unity Using C#

Creating Game Objects Objects can be created from either the drop down Game Object menu at the top of the program or, to a lesser extent, from the drop down create menu in the hierarchy window. You can create a variety of different types of objects, the basics with very brief descriptions being: Empty Objects- with no properties other than its transform properties. Particle Systems- special effects that are generated within systems Cameras- to view the game through GUI Text- to show text in the game Lights- to light the game in a variety of ways including from a source, or in one direction Shapes- generic game objects such as cubes, spheres, cylinders, etc.

Creating Components Components are created in a similar way to game Objects. Either through the drop down Components menu or, to a lesser extent, through the drop down create menu in the project window. There are also a variety of different components, again some basic ones with very brief descriptions are: Meshes- for wrapping textures to your objects Effects- such as particle systems and trails Physics- components involved in unitys physics engine such as: o Rigidbodies- the physical properties of an object o Colliders- the area where objects collide with other physical objects o Controllers- physical controllers for character in the game Audio- listeners and sources for producing audio in a game Skybox- the background of the game as seen by the game camera Scripts- different programs to add to objects within the game to perform specific tasks

10

Learning Unity Using C#

C# in Unity
All Scripts include at the top: using UnityEngine; using System.Collections; (in some cases, just using System;) and after that, usually start with: public class ScriptName: MonoBehaviour {

Access Modifiers private- variables are only accessible in the script public- variables can be assigned and changed in the inspector window, as well as accessed by other scripts with the proper referencing. static- used to create global variables

Variable Types int- a non-decimal number float- a decimal number, assign by adding an f after the number Example: char- a character string- a string of characters bool- a true or false Boolean variable Vector3- a float with 3 components, an x, y, and z component usually, commonly used for position, rotation, and scaling of an object Transform- a transform component to a game object
11

float num = 12f;

Learning Unity Using C#

GameObject- an instance of a game object GUIText- a GUI text object Material- a material component PhysicMaterial- a physic material component Queue<VariableType>- a generic queue included in System.Collections.Generic

Specific Examples TimeSpan timespan = DateTime.Now.TimeOfDay; - used for analog time Later uses: timespan.TotalHours timespan.TotalMinutes timespan.TotalSeconds DateTime time = DateTime.Now; - used for discrete time Later uses: time.Hour time.Minute time.Second Quaternion.Euler(float,float,float) - used for rotations Randome.Range(float,float) - used to generate a random float between two floats Randome.Range(int,int) - used to generate a random int between two ints Input.GetButtonDown(ButtonName) - detects if a certain button is pressed Input.GetButtonUp(ButtonName) - detects if a certain button is released

Methods void Start() - used when object is first instantiated void Update() - used when you want the object to update each frame void FixedUpdate() - used when dealing with the physics engine, updates via time instead of frames void OnTriggerEnter() - used when you want something to happen when another object triggers the one with this function void OnTriggerExit() used when you want something to happen when another object exits the trigger zone of this object
12

Learning Unity Using C#

void OnCollisionEnter() used when you want something to happen when another object collides with this object void OnCollisionExit() used when you want something to happen when another object stops colliding with this object

Accessors .active gets if the game object is active or not .enabled gets if the game object is enabled .emit whether a particle system is emitting particles or not .gameObject gets the game object .transform gets the game objects transform .localPosition from a transform, gets the position of that transform .localRotate from a transform, gets the rotation of that transform .localScale from a transform, gets the scale of that transform .Length length of an array .x first component of a Vector3 .y second component of a Vector3 .z third component of a Vector3 .rigidbody gets the rigidbody of an object .VelocityChange used after ForceMode, changes the velocity .Acceleration used after ForceMode, accelerates the velocity .isKinematic gets if a rigidbody is kinematic (doesnt use physics) or not

Built In Functions .ToString() gets the string of the object


13

Learning Unity Using C#

.ToString(f0) gets the string of a float formatted a certain way .ClearParticles() clears the particles in a particle system .Enqueue(VariableTypeOfQueue) used in generic queue to add an object .Dequeue() used in generic queue to remove an object .Peek() used in generic queue to look at but not remove the next object .AddForce(Vector3,ForceMode) adds a force to a rigidbody

14

Learning Unity Using C#

References
Unity Basics Video Tutorials http://unity3d.com/support/documentation/video/

2D Gameplay Tutorial http://unity3d.com/gallery/demos/demo-projects

Unity C# Tutorials http://catlikecoding.com/unity/tutorials/

15

You might also like