5 ~ Intro to Unity3D

This one is for Windows or Mac people.

Unity3D, the free tool that does more out of the box than my teenage brain (from many, many years ago) could have ever hoped for. You can prototype an idea in minutes, try to build a game variant just for you, or build a highly successful game* distributed on one of the most popular game distribution sites (you know the one, with the red knob sticking out of the guys head). One of my very favorite games, which shows just how high end of a game you can make with Unity, is Kerbal Space Program, which combines great graphics, high end physics simulations, and so many other features.

* Note, a highly successful game takes a team of people, usually years of development, and often a road littered with dead projects behind the team.

Unity delivers cross platform distribution via .NET C# (using the Mono Project), with an easy to grasp, and very flexible graphics engine. The Mono Project was created to be an open source .NET implementation years before DotNet Core was created. For the more advanced C# coders in the audience, you may get a feeling of living in a tiny house while coding with Unity3D. This is because they have implemented a subset of .NET, in order to control performance, and keep things focused. Take a deep breath, remember that you don’t actually need that language feature from your big data work, and have fun!

(Unity actually supports several programming language options, just use C#, you’ll be happier in the end)

This project shows only the most basic getting started type things. However, it still shows just how quickly you can get something going.

Here, we will look at some objects on screen being controlled by basic behavior code.

Let’s get rolling with some hands on!

0.) Install Unity3D

The free, personal edition can be downloaded from: https://store.unity.com/download?ref=personal

If you want to explore more about unity, here is their Home Page.

If you installed the Unity3D option for Visual Studio 2017, you can do all of your editing there, as well as, even debug your code while it is running in the Unity Editor interface. Don’t worry, if you didn’t, you can update VS2017 with that feature now! Otherwise, and if you are on a Mac, you will be using the open source IDE MonoDevelop which is installed automatically with Unity, and has a similar (if not as advanced) feature set including debugging code running in the Unity Editor.

 

1.) Be Aware of Documentation

Normally, I’d expect you to either ask a question, or use your search engine skills to find the software documentation. (or wait for a future article that’ll dig more depth in to a topic) However in this case, Unity3D is a huge thing to learn.

First off, here is the general User Manual for using the software. In this article, I’ll try to give you the basics, but there is much to learn.

Second off, here is the Scripting API Manual, which explains all of the built in language additions that you have access to in C#.

 

2.) Fork and Clone

Fork the code from here: https://github.com/DeveloperGym/DevGym_Project5_UnityIntro

 

3.) Run the Code/Simulation!

Open Unity. Select the folder where you cloned on your local system. Click the universal triangle “Play” button at the top. This will do a quick compile of the code, and then switch the editor to simulation mode. You will see a couple of things moving on screen. You will also see two buttons in the top corners of the simulation. Clicking Next and Previous will move the camera to each of three predetermined positions. Each of the three positions shows slightly more advanced motion techniques.

First position, is very basic movement. The red sphere moves up and down in a rigid motion. The smaller cube orbits the larger cube using a faked orbiting style. The final item implements both motions on the individual spheres with no code changes, truly showing off the power of CES. (more in depth later, in section #5, In Depth)

Second position, we have more fluid motion. The motion here is also accomplished by only a few lines of code, but it already looks better. The red sphere has less sudden of direction changes. With a bit of investigation, even smoother motion can be achieved. When we look at the code later, the small cube is doing a proper circular orbit calculation (did you know, that in order to keep the same side pointing at the Earth, the ISS basically dips the nose of the station 4 degrees per minute, if it didn’t, it would act just like the small cube, keeping the same orientation). Once again, the third object embodies both pieces of behavior without a code change in order to achieve a new effect.

Third position, shows an object that is randomly picking a target (the white half spheres on the ground), turns toward the chosen target, and moves towards it.

Clicking the “Play” button again will stop the simulation.

 

4.) Take a moment

Pop over to Position 2. Look around. Objects have soft shadows. Nice depth effect to the objects at Position 1. While the colors and set up are obviously artificial, it still looks good. And you are viewing this in a tiny window in the editor, not even at full screen. Also, all of these settings are straight from the default settings of a new project. I added the objects and the code, but I didn’t touch any of the visual settings. Throw in shaders and some actual effort, and it can look amazing.

 

5.) In Depth

(this will be a big one, as i’ll be showing you the basics of the Unity Editor, as well as short explanations about how the code accomplishes the tasks you see on screen)

Opening Notes:

A key piece to understand about the way that Unity implemented objects in their system is that they placed an emphasis on a “Component Entity System” (CES). Go ahead, hit up your favorite search engine. I’ll just wait here. (you may have to throw in a “-framework” to your search, to get rid of the Entity Framework results) … … … Now that you are back, the TLDR; of a CES, is that instead of inheriting until you are not only painted in to a corner, but somehow there is also a brick wall around you, small focused classes with limited behavior are created and brought together. Add them to a core, mostly non-functional base object, and you have super flexible re-usability, and no brick walls. Unity is not a pure CES because the core object does have some functionality, as a nod to practicality. Very few of us can live in the rarefied academic air CES came from.

Here is my favorite article on how inheritance is misused: Inheritance is evil, and must be destroyed

Interface Basics

1.This is the visual editor. You can click on objects, or hold right mouse button and rotate the camera. Keyboard arrow keys to move the camera around. If you click on an object and hit the “F” key, it’ll focus on that object.

2. Basic testing controls.

3. Object Structure. This is where all of the objects in the scene are listed.

4. Asset Folder. Where all the files in the project are organized.

5. Inspector. When you click on an object in the view, or Object Structure, all of the relevant details about the object appear here.

 

Object Structure

This is a parent/child relationship of in-scene objects. When a parent is moved, the child objects come along for the ride. It is also a handy way to organize multiple objects.

You’ll note that for the “Basic Motion” orbit objects, they use an invisible parent object that really just spins in place, making it look like the child objects are orbiting. The same objects in “Better Motion” use a bit of math, to simulate a better orbit.

Clicking on an object, gives you all of the details in the Inspector section. While the simulation is running, you can change the values in the Inspector, and see the changes on the screen instantly. Also, all changes in the Inspector while in the simulation are temporary.

 

Folder Structure

This shows you some of the folders in the project on the hard drive. You can put images, objects, sounds, source code, and other files in here. Many types of files will be recognized and converted to a compatible format to be used in the scene.

It is good to keep some organization so you can find things later. I tend to follow a pattern of:

  • A_Prefabs: Stores objects i want to reuse again later
  • B_Scripts: The behaviors that the objects exhibit
  • M_Materials: Where the pieces to stop everything from being pure white are stored
  • S_Scenes: Where the different scenes of the project are kept. Scenes are: Your game opens on a main menu. You click a button to start a new game, and Level 1 loads up. You beat that, and progress to Level 2! That is three different scenes right there.

You can nest folders as much as you need, and name them what you want.

 

Getting To The Code

Each behavior on an object is called at different times. The “Awake” method is called once, when the object is first brought in to the scene. The “Update” method is called every frame, because of this, you will see “Time.deltaTime” used a lot. This variable gives you the amount of time since the last frame was rendered, which is the info needed to create a smooth animation, no matter if you are on a fast or slow system.

Also take note, when you click on objects in the Inspector, you can see which scripts (behaviors) are attached to that object. You can add and remove behaviors in the Inspector, or change values (like the bounce height from the first line of objects). Just make sure to do it while NOT running the simulation, otherwise your changes will be lost.

  • ActionFakeBounce.cs – Uses a simple Translate method to move an object up and down. You can control the high and low points in the Inspector window after you click on the red sphere.
  • ActionFakeOrbit.cs – This fakes something that looks like an orbit, by slowly rotating an invisible parent object. The actual small cube is a child of the object with this behavior (script).
  • ActionBetterBounce.cs – This follows a similar pattern, but instead of a jerky motion, it uses SmoothDamp to smooth out the ends of motion.
  • ActionBetterOrbit.cs – Instead of an invisible parent, we are using some basic trigonometry to move the object in a circular motion.
  • ActionMoveAround.cs – This is the third motion behavior. It takes a list of positions (visibly shown by the white half spheres on the ground), picks a random one (that isn’t the one it is currently at), turns towards it, and then has a basic motion to get it to the target (the motion could be a lot better, this is just fun). Lather, rinse, repeat. 🙂

 

Final Notes

Unity3D is a great tool to scratch that 3D interactive simulation itch. Have fun, and let me know what else you’d like to see. 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *