Unity Tips

for engineers

background image credit: all-free-download.com

About Me

Hello Unity!

so much windows

Editor Panels

image credit: gamedevacademy.org

Editor Panels

  • Project - Your files
  • Hierarchy - Your game level
  • Scene - Also your game level
  • Game - Your game (when it's running)
  • Console - Messages from Unity and your scripts
  • Inspector - Details about whatever you've selected

Getting Around

  1. Don't try to get by without a 3-button mouse!
     
  2. Double-click on something in the Hierarchy to see it in the Scene.
     
  3. You can use Flythrough mode if you like it better.
     
  4. You can "reset" your view by selecting the main camera and choosing GameObject->Align View to Selected.

Unity Concepts

A scene is the basic file you are working on in Unity.

 

A scene is made up of GameObjects.

What is a GameObject?

It's just a container for components.

 

You can make a GameObject act like a light, a camera, etc. by adding the right components.

 

Everything you can see in the Hierarchy or Scene is a GameObject.

 

Everything you see in the Inspector is a Component.

Unity Concepts

It's good practice to use empty GameObjects to group your GOs and keep your Hierarchy tidy.

Unity Concepts

But notice that a child GO will inherit its parent's local transformation matrix – rotation, scale, etc.

When you're scripting Unity, you're not running the show. You're hooking into Unity's core loop.

 

Here is a simplified version showing the overall order of the loop.

The Core Loop

Unity gives you named methods to tie into different parts of its core loop from code and tell it what to do.

 

You can find the whole list of hooks by searching for "MonoBehaviour lifecycle".

The Core Loop

Engineers often want to roll their own physics, etc., rather than understand how to tie into Unity's events.

 

This is usually not a good use of your limited time.

The Core Loop

Pro tip: Understand the difference between FixedUpdate and Update.

 

Notice that Update is tied to the Input phase. FixedUpdate can run several times per frame.

 

It's common to set flags in FixedUpdate and then respond to them in Update, and vice versa.

The Core Loop

This is a symbolic representation of GOs in a hierarchy.

Unity also keeps lists of relevant active components for the different parts of its core loop.

So in the physics loop, Unity starts by dispatching FixedUpdate to all MonoBehaviours. Then it finds all the physics system components.

It finds all the Rigidbody components and updates their position, velocity, etc. It looks for collisions and dispatches OnTriggerEnter, OnCollisionEnter, and so forth to attached MonoBehaviours.

Then it finds all the MonoBehaviours and calls Update.

Then it finds all the renderers and updates them, dispatching various rendering events.

There are other built-in Unity components for networking, animation, pathfinding, particles, audio, etc.

Unity Components

  • Audio Source
  • Animator
  • Nav Mesh Agent
  • various UI components

You're likely to use:

About Collisions

Every collision requires three components:

  • A Collider on one GameObject
  • A Collider on another GameObject
  • A Rigidbody on at least one of the two GameObjects

About Collisions

There are two types of collisions:

  • Regular collisions act like real-world collisions: objects bounce off each other.
  • Trigger collisions detect when an object has entered a particular space. They are used to do things like open a door when the player comes close to it.

About Collisions

  • Any collision is either a trigger collision or a regular collision.
  • It is a trigger collision if either collider has the "IsTrigger" box checked.

About Collisions

  • If it is a trigger collision, the events will be
    • OnTriggerEnter
    • OnTriggerStay
    • OnTriggerExit
  • These functions will just receive the incoming Collider as an argument.
  • They are called on all attached scripts on both GOs.

About Collisions

  • If it is a regular collision, the events will be
    • OnCollisionEnter
    • OnCollisionStay
    • OnCollisionExit
  • These methods receive a Collision object, which has information about the collision.
  • They are called on all attached scripts on both GOs.

About Collisions

Read this page and this page in the Unity user guide before you try to work with collisions.

 

Really.

 

You've been warned!

About Collisions

For animated characters you will often have a Rigidbody on the parent GO and then several colliders on the children (arms, legs, etc.)

 

If you do this, stick test scripts on all of the different GOs and make sure you understand which objects are getting the messages.

Debugging Collisions

  • Log something to console immediately inside your collision function(s).
  • Check that you have all three components and that isTrigger is set correctly.
  • Run the game and manually drag one object onto the other.
  • Then debug your collision logic.

Organizing Your Project

  • Choose naming conventions for files, GOs, etc. In Unity/C# everything is PascalCase except variable names.
     
  • Use a namespace for your code. This prevents collisions with anything you import from the Asset Store (or elsewhere). You can change the default C# template in Visual Studio.

Organizing Your Project

  • Make all (or nearly all) of your GOs prefabs. Prefabs are files and can thus be tracked separately for version control. Prefab Variants may be helpful.
  • Keep your Hierarchy tidy.

Help I Can't Art

Don't fight it.

Here are things you can do as an engineer:

The Unity Asset Store isn't just models. There's things like

 

soft-body physics

better controller integration

third-person character controllers

pre-rolled inventory systems

and much more.

 

If it's a popular asset, that means it has already been tested extensively and will likely have features you wouldn't have time to write.

Unity Demo

Add, remove, enable, disable, modify components

Create and modify a Material

Getting around in Scene view

General Unity Resources

Getting Started

"How to make a Video Game" by Brackeys is an excellent quick start, with 10 short videos of <15 minutes apiece.

General Unity Resources

This is a curated collection of resources for learning more about/getting better at Unity and game development.

 

It's primarily meant for students just finishing my Intro class, but there's useful links for all levels.

General Unity Resources

I particularly recommend

"Learn Advanced C# Scripting in Unity 5 for Games".

  • the content has aged well (despite the name)
  • it's not advanced (despite the name) but it has excellent, practical advice about using Unity effectively.

General Unity Resources

This page offers a pretty comprehensive overview of tutorials and resources for Unity, and it's frequently updated:
XR Unity Tutorials List

 

I particularly recommend these sites:
Ray Wenderlich
NoobTuts

Made with Slides.com