You are viewing our Forum Archives. To view or take place in current topics click here.
Getting Started With Unity 3D Game Engine
Posted:

Getting Started With Unity 3D Game EnginePosted:

ODST_107
  • Resident Elite
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Hey guys, I've seen a few posts going around about how to start making indie games and where to start. So here's a getting started guide I'm working on for Unity 3D.

Why use Unity?
  • It's free
  • It's fairly easy to get started with
  • It has a wide range of assets it can import
  • It's actually really powerful


Things to keep in mind before you begin:

  • Don't expect to hit it big right off the bat, good games take time and great games take even longer!
  • You won't be rich from an indie game created with free software. Licensing restrictions mean that if you use a free game engine like Unity's free edition, you are legally not allowed to charge money for your creation.
  • Patience is a virtue. These things take time. Not only to create, but to learn as well.
  • The KISS principal. Keep It Simple Stupid. The simpler the idea is at the start, the easier it will be to implement and then upgrade later on.
  • Don't be afraid to ask for help. Even professionals need a helping hand every so often.


The best approach to almost any project is to spend most of your time planning. When starting a project that involves programming I like to sit down and layout everything that I'll want the program to do and then section it out. This allows you to always keep your end goal in mind and have a better idea on the steps you need to take to get there.

This starts the creative part of the tutorial. Here I am going to lay out a simple project to get your feet wet with in Unity.

Part 1: The Idea


There are lots of things to think about when starting a game. Will it be first person or third person? Is it 3D or 2D? Single player or multiplayer? What's my story? Why am I here? Where am I? What obstacles will I face?
These are all very important things you have to keep in mind and decide on before you can start putting your game together.

The first step we are going to consider is multiplayer. Networking in Unity is relatively simple, but we are going to keep this project single player. This is a very important step in the decision making process. If you decide to eventually create a multiplayer game it is important to layout the networking end first. This way when you actually start working on the game, you can test your networking to ensure proper functionality early on. It will make your life easier.

Now that we know our game will be single player, we need to decide if it should be first person or third. There are pros and cons to both. For simple games, single player can be better as it requires one less art asset and the animation that would go with it. This makes it easier to set up your character. Third person is better for RPG style games where you want to be able to see the items and accessories that you find in your game world. Third person can also give you a little better perspective of the world that your character resides in. For simplicity reasons, we're going to use first person.

There are other things you can do for character and camera control like you would see in a 2D side scroller, but in this tutorial we are going to cover first person 3D.

While the rest of the things on our initial list are very important for game design, we are going to skip them for now and simply dig into working with unity and a few other programs to create a beginners game world.

This brings us into the programs section of the tutorial. Here I will outline which programs we will need and how to install them.


Part 2: Programs and Requirements


We will need a few more programs besides unity to start creating game content.
These programs are:

  • Unity 3d Free version
  • Gmax, 3dsMax, Maya, or Blender (3d modeling)
  • Paint.net

For the best performance I am using a Windows 7 computer with 8GB of RAM and 3.1GHz processor, but just be sure your computer can run the minimum requirements by these programs.

I am currently using 3dsMax as my modeling software, but for this tutorial I am going to be using Gmax. They are almost the same program, but 3dsMax has more features built in. For these demonstrations Gmax will do just fine.

To start we need to download and install these programs. Go to the following links and download the installers.
[ Register or Signin to view external links. ]
[ Register or Signin to view external links. ]
[ Register or Signin to view external links. ]

Gmax and Unity will ask you to register. It's free and takes about five minutes to do both and they don't bother you with tons of spam emails.

After downloading the installers follow the instructions for installing them. With Unity you want to be sure to install the demo project and all other packages. They will come in handy later on when we actually start using unity.

Gmax and Paint.net are self explanatory for installation. Once you have these installed we can actually begin to take a look at the Unity platform


Part 3: Getting Familiar With Unity


Go ahead and start up Unity now.
Your screen should look a little something like this. It may look a little different if you have played with Unity before and edited the layout.
[ Register or Signin to view external links. ]

It may have also started up the demo project. This is fine and dandy if you want to poke around, but for now we're gonna start a new project. So click File>New Project and type in the name of your project and enable all of the options below. We'll use these to simplify the player creation process.

In the upper left corner we have four buttons. Pan, Move, Rotate, and Scale. The two buttons next to it we wont' have to worry about.

In the middle at the top we have three buttons. Play, Pause, and Advance Frame. These are pretty straight forward. Play starts the game, Pause, pauses the game, and Advance frame allows you to see how things are working one frame at a time. For this tutorial we will only use play and pause.

There are two drop downs in the upper right that we wont worry about. However you might want to eventually play with the layout and set it up to your liking.

On the left side of the screen we have the Hierarchy panel. This panel lists all the objects placed in the game scene and their children.

In the bottom left is the Project view and the Debug Console. The project view is where you can find all the assets in your project folder. This is where you will go to get imported assets to put them into your scene. The debug console will show you errors in scripts including what the error is, which script it's in, and what line it's on. This will become useful later on when we start into scripting.

Dead center is the Scene view and the Game view. The scene view is an interactive panel you can use to place and move objects in your game world (called the scene). The game view will show you what the main camera sees at any given point in time.

Lastly, on the right is the Inspector. The inspector will show you details about the object you have selected either in the hierarchy or the scene view. Here you can manually edit things like position, rotation, and scale for precision placement.

As a simple example, click on the "Main Camera" object in the hierarchy view. Your inspector should now look like this:

As you can see, there are five items in the inspector: Transform, Camera, GUILayer, Flare Layer, and Audio Listener. These are what we call components. Components can do all sorts of things, from illuminating the object with a light, to giving it a rigid body that other objects will collide with.

To start playing around and getting familiar with Unity's controls, click on the scene view, and then at the top of the hierarchy click create, and then click "cube". If you haven't moved the camera, a cube should now be created in front of the camera. If you click on the camera you can see a preview of what it sees in the bottom right hand corner of the scene view. You should see a square in the middle of the preview. Use the buttons at the top left corner of Unity to move and rotate the cube to see it at different angles. Play around with this until you are comfortable moving objects around the scene view.
[ Register or Signin to view external links. ]



Part 3.5: Keyboard Controls


Previously you got familiar with using the buttons in the UI to move objects around in the scene view. Now I am going to teach you how to actually navigate the scene view, but first we need to go over the basic controls for moving around the scene view.

  • Q: Pan
  • W: Move
  • E: Rotate
  • R: Scale
  • F: Focus selected object


Mouse controls:

  • Left Click: Select or Pan
  • Middle Click: Pan
  • Right Click: Free Look

With the right mouse button held down, you can also navigate the scene view with WASD to move, SHIFT to go faster, E to go up, and Q to go down. This will make it easier to navigate to a closer view of certain objects.

Play around with the controls and get a good feel for getting your way around in the scene view. Create a few more cubes or other objects like you did before, just don't get too attached as we will be deleting everything from the scene in just a minute.


Part 4: Building Your World and Living In It


Go ahead now and highlight everything in the hierarchy view and delete it. In the hierarchy view select create, and select "Terrain". Terrains are editable planes that you can use to simply shape your world.
[ Register or Signin to view external links. ]

In the scene view, navigate your way up over the newly created terrain like the image below and then click the Raise/Lower terrain button in the inspector.
[ Register or Signin to view external links. ]

This allows you to raise the terrain by clicking on it where you want to raise it. To keep it simple, we're just going to put some mountains around the edge of the terrain to keep our player from falling off.
[ Register or Signin to view external links. ]

We're going to stay simple for the time being and not worry about textures for the terrain and skip right to the character. To do this, we need to find the pre built character in our assets folder. This will be under "Standard Assets>Character Controllers" called "First Person Controller". Now drag the first person controller from the assets panel into the scene view and position it somewhere in the center of the terrain and ensure that no part of it is below the terrain, or else you'll fall through the map!
[ Register or Signin to view external links. ]

Now you can click the play button to view your game so far. You'll notice that you can only see the contrast of the mountains against the blue background. This is because there is no light source in the scene to illuminate the terrain and the rest of the objects that will later be placed on the terrain. To fix this we are going to create a "Directional Light" to illuminate our scene. There are four types of lights in unity, Directional, Point, Spot, and Area lights. Right now we're going to focus only on the directional light. Directional lights work great for moon light and sun light. Place a directional light in your scene and set its rotation to X of 45, Y and Z of 0. Then set its intensity to 0.35 to give it a nice mid day sort of look.
[ Register or Signin to view external links. ]

Now if you hit play you should actually be able to walk around your newly created world and survey the land.

To make it a little easier to see, click on the terrain and then in the inspector click on the paint brush icon. Below that should then be a box titled textures and a button that says edit textures. Click on edit textures, then add new texture, and then in the box that should now be on your screen, click select in the box titled "Texture". Find a texture to your liking and click add. Your terrain will now be textured. You can add more textures and use the paint brush to paint different textures in different areas.



Part 5: Adding a Few Items


This next part is all about what you want to do with your game. Everyone has different ideas for things they want to do. Since I don't know what's in everyones heads, I'm going to put together a simple game that has you wandering around the world trying to collect spheres and dodging vicious yellow boxes. Do to this we are going to need to set up a few things.

First off we need the objects we will be using for the spheres and the yellow boxes. To get these, create a new sphere and a new box. Rename the sphere "food" and the box "enemy".
Next we need to go into the assets browser, click new, folder, and create a folder called "Prefabs". Here we will store things that we will use multiple times. Prefabs make it really easy to maintain objects that you will use multiple times in a scene. The advantage comes when you find that you need to edit something but don't want to edit every single one. You can make your edits and then apply them to all. Now we need to drag both the "food" and the "enemy" into the prefabs folder and then delete them from your scene. This will uphold the prefab integrity.

To start off lets focus on the "food" item. The idea of the food is that when you walk up to it, you pick it up and eat it. Obviously this would require a complex animation so we're going to do it Minecraft style. There's a few changes we need to make to the object itself, so open your prefabs folder and select the "food" item for inspection. First thing we need to do is to scale it down to half it's original size. So in the transform panel set the X Y and Z scale to .5. This will give it a much better proportion to our player. Next, at the bottom of the inspection panel there is an add button. Click on Add>Physics>Rigid body. This will give our food object gravity. Now we need to add another collider to our "food" object, so click Add>Physics>Capsule Collider. It will ask you if you want to replace the current one, you want to add it, not replace it. Now we need to edit the Capsule collider we just added. The first thing we need to do is enable "Is Trigger". This allows us to then detect when another collider enters inside this capsule instead of just colliding with it. Next we need to make it a little bit bigger. To do this we will edit the radius of the capsule, NOT the height. A good radius for this is 2. Not too big, not too small. This way you have to walk over it, but not directly over it.

Now we're going to do something similar with the "enemy" object. Select the "enemy" object in the prefabs folder. We want to add something similar to the "food" but not quite. First we are going to add another collider to it, this time a sphere collider. Like the last one, select the Trigger box, and make the radius about 1.5. We are then going to add what is called a Nav Mesh Agent. This is going to save us a LOT of time with our enemy script.

Speaking of scripts, that's next up on the agenda.


Part 6: Scripting


Scripting can seem just a little intimidating for a beginner. If you have any experience with Javascript or C# then you won't have any problems with scripting. If you are new to scripting however, it may be a bit of a learning curve. My suggestion would be to go to [ Register or Signin to view external links. ] and read up on Javascript. I'm going to use C# for my examples simply because it's what I am familiar with and what I know. There are plenty of good C# tutorials out there to get you started, but if you write your code exactly like mine it will work for you.

The first thing we need to do before we can get to actually scripting is setting up our tags. Tags are used to find game objects from code. The only tag we need to worry about right now is the "Player" tag that is already set up in the inspector. Obviously we need to assign this to the object we will be controlling. So in the hierarchy click on the first person controller, and then in the inspector click on tags, and scroll down to "Player" and select it. Now we can reference our player from code.

To keep things rather simple we are only going to use three scripts: foodController.cs, enemyController.cs, and inventory.cs.

To create these click on create in the project view and create three new C# scripts and name them accordingly. To edit these click on one of them in the project viewer and then click open in the inspector. This will open MonoDevelop with your project files. MonoDevelop is a fairly decent editor that DOES have auto complete. The nice thing is that it syncs your project folder so theres no need to open multiple MonoDevelop windows.

MonoDevelop

MonoDevelop is simple to use and work with. On the left you have all the code files in your project. C# scripts are under "Assembly-CSharp>Assets" and JS scripts are under "Assembly-UnityScript>Assets". But we will only be using C# for this example.


Alright, now lets dive into the code portion. With MonoDevelop open, find and select the "enemyControl" script and open it for editing.

This is what your code should look like

using UnityEngine;
using System.Collections;

public class enemyController : MonoBehaviour {
    GameObject player;
    NavMeshAgent nav;

    //this is called when the object first starts
    void Start(){
        player = gameObject.FindObjectWithTag("Player");//this will give us a link to our player
        nav = getComponent<NavMeshAgent>();//this gives us a link to our NavMeshAgent component of the "enemy"
    }

    //this is called every frame
    void Update(){
        nav.destination = player.transform.position;//Every frame the navAgent is updated with the player's position
    }
   
    //this is where the trigger comes into play
    void OnTriggerStay(Collider other){
        if(other.tag == "Player")
            player.sendMessage("doDamage");//start a function that we will write in the inventory script
    }
}


Next we need to edit the "foodController" script. This one will be a little easier and look something like this


using UnityEngine;
using System.Collections;

public class foodController : MonoBehaviour {

   // Use this for initialization
   void OnTriggerEnter(Collider other)
   {
      if(other.tag == "Player")
                {
         other.gameObject.SendMessage("addFood");//adds some food to the inventory
                        Destroy(gameObject);//removes the object so you cant pick it up again
                }
   }
}


This one is nice and short. Simply if the player collides with it, we add the food to the inventory. Which leaves us with our last script, "inventory".


using UnityEngine;
using System.Collections;

public class inventory : MonoBehaviour {
   
   public int food = 100;
   public int foodMax = 100;
   public int foodAdd = 10;
   public int foodSubtract = 5;
   // Use this for initialization
   void Start () {
   
   }
   
   // Update is called once per frame
   void Update () {
      if(food <= 0) Debug.Log ("Youre dead!");
   }
   
   public void addFood()
   {
      if(food < foodMax)
         food += foodAdd;
      Debug.Log (food.ToString());
   }
   
   public void doDamage()
   {
      if(food < 0)
         food -= foodSubtract;
      Debug.Log (food.ToString());
   }
}


The inventory script isn't too terribly complex. It's kept simple enough to expand on later which we will do, but right now all that we want is a simple health tracker, which is what the food variable takes care of. We'll get into actually putting a health meter in later.

Next we need to attach these freshly minted scripts to our game objects. Open the food item from the prefabs folder, and drag the "foodController" script from the assets folder into the "food" inspector. Next do the same with with "enemyController" script and the "enemy" object. The inventory script is a little different. With this one you need to drag it onto the "First Person Controller" in the hierarchy view and it will be added to the character.

This tutorial is a work in progress so keep checking back for more of the tutorial and by all means feel free to elaborate off of it. Creativity is all about breaking out of the box and finding your own style.


Last edited by ODST_107 ; edited 1 time in total

The following 2 users thanked ODST_107 for this useful post:

iyop45 (09-19-2013), Tomoyo (09-18-2013)
#2. Posted:
ConnorHamilton
  • TTG Senior
Status: Offline
Joined: Apr 27, 201113Year Member
Posts: 1,718
Reputation Power: 67
Status: Offline
Joined: Apr 27, 201113Year Member
Posts: 1,718
Reputation Power: 67
You can also connect it to WarZ and other games and make real time edits to the world.
#3. Posted:
Vectorizing
  • Prospect
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 648
Reputation Power: 27
You won't be rich from an indie game created with free software. Licensing restrictions mean that if you use a free game engine like Unity's free edition, you are legally not allowed to charge money for your creation.



This can be true with SOME free programs. However with Unity 3D you can distribute your games for any price for FREE, BUT if you make $100,000 or more with your game your have to buy Unity 3D Pro.

"If you make more than $100,000 with the free version of Unity you have to buy a Pro license."

Source: Myself.
Backup Source:
[ Register or Signin to view external links. ]
#4. Posted:
AmishTech
  • TTG Senior
Status: Offline
Joined: Nov 14, 201014Year Member
Posts: 1,031
Reputation Power: 61
Status: Offline
Joined: Nov 14, 201014Year Member
Posts: 1,031
Reputation Power: 61
Man when I hear the K.I.S.S Rule I think of Dave Ramsey. Hahaha
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.