Learn 3D Graphics

Game Development with Unity + Blender

Learn the basics of creating a game in Unity with assets created in Blender.
  1. Create a Unity account at Unity 3D in order to access Unity
    1. Unity can be used for free for educational purposes and also commerically, as long as you are not making $100,000 or more using Unity - after that point you need to pay for a license
    2. Choose and submit your account information
      1. Use your school email
      2. Write your password down on paper or in your phone or etc.
      3. Choose a school appropriate and unique username
      4. Feel free to use a pseudonym for the name
    3. Confirm your email address (if the Unity confirmation email doesn't show up in your inbox within a couple minutes, check the spam folder)
    4. In the "Individual" tab, select "Get Started" under "Personal"
    5. Agree to the terms and conditions to download "Unity Hub"
  1. Download "Unity Hub" from the Unity Website
  2. Right-click on the Unity DMG file to open it
  3. Drag the Unity Icon and drop it over the "Applications" folder
  4. Right-click on Unity Hub to open it with Finder
  5. Sign in with your user account information
    1. If the sign-in doesn't come up automatically, click on the profile icon and then select "Sign In" This image is missing
  6. If you are asked to select a microgame, choose "blank 3d game"
  7. If Unity launches, go back to the Unity Hub app to finish the installation process
  8. Make sure the personal license is activated:
    1. Click on the gear icon and go to "License Management" This image is missing
      1. Select "Manual Activation" This image is missing
      2. Select "Save License Request" and choose a location to save the request file This image is missing
      3. Follow the link to the "Unity Activation" page (sign in if needed)
      4. Click on "Browse" to select the license request file (.alf format) This image is missing
      5. Select "I don't use Unity in a professional capacity" This image is missing
      6. Download the license file This image is missing
      7. Back in Unity Hub, click on the "Load" icon (...) to select the license from the downloads folder This image is missing
    2. Exit Preferences by clicking on the back arrow This image is missing
  9. Install the modules:
    1. With the left "Installs" tab selected, click on the the more options button (3 dots) on the top right corner of the installed Unity version This image is missing
      1. Select "Add Modules"
      2. Check "Visual Studio" and "Mac Build Support" (if your computer is not a mac, also select the platform you are using - ie. Linux or Windows) This image is missing Additional platforms/modules can be easily be installed later on if needed in the same way.
      3. Select "Done" and let the installation finish
      1. click on the "Add" button This image is missing
      2. Select the version of Unity to Install and then press "Next"
      3. Check "Visual Studio" and "Mac Build Support" (if your computer is not a mac, also select the platform you are using - ie. Linux or Windows) This image is missing Additional platforms can be easily be installed later on if needed.
      4. Select "Done" and let the installation finish
  1. Select the "Projects" tab from the sidebar annd press "New" This image is missing
  2. Select "3D"
  3. Choose a project name and location
  4. Select "Create" This image is missing
  1. Create 3 scenes: a game scene (ie. Level1), a lose scene, and a win scene:
    1. In the "Project" window, inside of "Assets" and then "Scenes", select the "SampleScene" and right-click to either delete or rename the scene descriptively
    2. Add additional scenes with "File → New Scene" or "Cmd+N"
    3. Save each scene "File → Save Scene" or "Cmd+S" in the Scene Assets folder for the project after it is created
    4. Use descriptive names without spaces or symbols (ie. Level1, Lose, Win or etc.) This image is missing
    5. Add the scenes to the Build Settings (so they can be accessed in the game):
      1. Open the Build Settings under "File → Build Settings" This image is missing
      2. Select all of your scenes from the Project window (click in the Assets folder and then use CMD+A) This image is missing
      3. Drag and drop the selected scenes into the box below "Scenes in Build"
        This image is missing
  1. In the Project window, double-click on the game scene (ie. Level1) to load it
  2. In the "Hierarchy window" go to "Create → Canvas" - any UI (user interface) elements must be created on the canvas This image is missing
  3. With the canvas selected, right-click and under "UI" select "Text"
  4. At the top of the Inspector, rename the text object from "Text" to "Timer" and press enter (always use descriptive names - it is helpful if you are working collaboratively and when you reuse your assets) This image is missing
  5. Lower down in the Inspector, in the type box, type "Timer" (or other placeholder text) into the text box This image is missing
  6. If the text is not visible, navigate/adjust the view of the "Scene" window as needed:
    1. Zoom to cursor position - Alt+scrollwheel
    2. Rotate view - Right-click+drag
    3. Flythrough mode - Right-click+hold with:
      1. W - left
      2. A - right
      3. S - forward
      4. D - backwards
      5. Q - up
      6. E - down
  7. With the text object active, set your text size and alignment as necessary in the Inspector Window:
    1. To increase the text box, click and drag on the values for width and height under "Rect Transform" in the Inspector or turn on the "Scale Tool" and drag the handles
      This image is missing or This image is missing
    2. To change the position to the upper left corner, click and drag the X and Y values in the Inspector or use the handles with the "Move Tool" active
      This image is missing or This image is missing
    3. Standard font, alignment and color options can be edited in the Inspector under "Text" This image is missing
  8. Save the Scene with "CMD+S"
  1. In the Project window, double-click on the "Lose Scene" to load it
  2. Add and customize your "Lose Message":
    1. Right-click in the Hierarchy to create an "UI → Text" object (the canvas will be created automatically)
    2. In the Inspector, rename your text object to "Lose Message" and press enter
    3. Change the text in the text box to a lose message (ie. Sorry - You Ran Out Of Time)
    4. Under "Rect Transform", increase the width and height of the text box to around 525 (width) x 70 (height) as a starting point
    5. Increase the font size to 50
    6. If your text is not visible either:
      1. Divide your message into multiple lines (use enter in the text box) and then increase the width/height of the text box as needed (height should be at least double)
      2. Or in "Text(Script) next to "Vertical Overflow", select "Overflow"
    7. Use the Scene Window's move/scale tools or the Inspector window to resize and position your lose message in the camera as needed
    8. Adjust other text alignment/display options in the Inspector as needed
  3. Add a background
    1. Select the "Main Camera" in the Hierarchy
    2. In the Inspector, change "Clear Flags" from "Skybox" to "Solid Color" This image is missing
    3. Click on the color in the "Background" field to access the color picker and select your background color This image is missing
  4. Add a button to restart the game
    1. Right-click on the "Canvas" in the Hierarchy and select "UI → Button" This image is missing
    2. Use the move tool to move the button below the lose message
    3. In the Hierarchy window, expand the button by clicking on the triangle to the left of button This image is missing
    4. Select the button's text object
    5. In the Inspector, change the text in the text box from "Button" to "Restart"
    6. Adjust other text settings as needed
  5. Press the play button to preview the scene - notice there is no functionality yet as there are no scripts
  6. Exit the game by pressing the play button again and save the scene with "Cmd+S"

About Scripts:

Scripting in C# is made up of 3 major components: variables, functions and classes. For a script to work, it must be attached to a game object - or be called by a script attached to a game object

  1. About Variables:
    • Variables can contain values and refer to objects
    • First letter is lowercase
  2. Variable Structure:
    • visibility keywordtypename
    • ie. public Light myLight;
  3. Most Common Variable Visibility Keywords:
    1. Public
      • Allows different objects to interact with each other
      • Accessible to other people
      • Accessible to other scripts
      • Accessible to other classes
    2. Private
      • Easier to maintain/track
      • Only accessible in the specific script
      • Only accessible in the specific class
  4. Variable Types:
    1. Numbers
    2. Text
    3. Reference to Unity Components
  5. Variable Names:
    1. Rules
      1. Can't start with a number
      2. No spaces
    2. Conventions:
      1. Start with a lowercase word
      2. Add additional words with the first letter capitalized
      3. ie. myLight
  1. About Functions:
    • Collections of reusable code that compare and control variables
    • First letter is uppercase
    • Can be used multiple times
  2. Default functions (run automatically):
    1. Awake()
      • Initializes variables that need an assigned value
      • Called when an new instance of the class/object is created/instantiated - usually the first frame of the game, or when something becomes active
      • Only works on an active object
      • Works on an active object even if the component is disabled
    2. Start()
      • Called when an object is active
      • Only works if the component is enabled
    3. Update()
      • For any logic that runs continuously
      • Called once per frame
    4. FixedUpdate()
      • For working with physics
    5. LateUpdate()
      • Like Update but called at the end of the frame
      • Called after Update
      • Good for triggering a secondary action that should directly folow something else (second loop)
  3. Function Syntax:
    • returned function typefunction name(function parameters){function body}
    • ie. void MyFunction () {
      myLight.enabled = !myLight.enabled }
    • Function names start with a capital letter
  4. A Few Common Function Types:
    1. void - nothing is returned (ie. we're not trying to calculate values or etc. and get data back)
    2. int - number without a decimal
    3. float - "floating" point value that can represent a range of values with decimals
    4. string - text
    5. bool - true or false
    6. Color
    7. Rigidbody
    8. Multiple types can be set with commas between
  5. Custom functions:
    • Custom functions are not called automatically like the default functions
    • They can be called from another script, or from inside a default function (ie. awake, update or etc.)
  1. About Classes:
    • Classes organize the collections of code
    • They combine variables and functions together to determine the properties of an object
    • Can be public or private
  2. Class guidelines:
    • Class name must match the name of the C# file
    • To add to a GameObject - Monobehavior class is needed (should be added by default)
    • Serialize by adding [System.Serializable] directly above the custom class
    • ie. [System.Serializable]
      public class DataClass {
      public int myInt;
      public float myFloat;

      }
  3. Custom classes:
    1. Custom classes must be serialized (or converted into simple data for Unity to read)
  • Comments are added in C# with //
  • Anything written after // in the same line, will not impact the game
  • Anything written between /* and */ will not impact the game
  • Comments are used for:
    • Keeping code organized
    • Leaving reminders for yourself
    • Leaving notes for other developers
  • Comments always display greyed out

Script to Change Scenes & Restart:

  1. Create a Script folder in the Project window (Create → Folder) This image is missing
  2. Name the folder "Scripts" and then press enter This image is missing
  3. With the Scripts folder selected, create a script (Create → C# Script) This image is missing
  4. Name the script - ie. ChangeScene
    The script is a text file that can be opened in Visual Studio and most text editors This image is missing
  5. In Preferences (Unity → Preferences → External Tools), change the "External Script Editor" to "Visual Studio" This image is missing
  6. Open the Script in Visual Studio by right-clicking on the name in the Project Window and then select "Open"
  7. Default scripts made in Unity are Windows based - we should convert the formatting to work for Mac without errors:
    1. Go to "Visual Studio → Preferences → Text Editor → General"
    2. Change the "Line ending conversion" to "Always ask for conversion" This image is missing
    3. Close out of preferences and if there is a warning notification at the bottom of the script, say "Ok" to convert the line ending for Mac (Unix)
  1. Understanding and changing the script:
    • The beginning of the script should always specify the game engine and the system so that Unity knows what resources to use
    1. Delete lines 1 & 2 (often you will keep these lines, but in this case, we are using Unity's scene management capablities ‐ not collections)
      This image is missingThis image is missing
    2. Click in the end of line one (after "using UnityEngine;") and press enter to begin a new line This image is missing
    3. In the empty line 2, add "using UnityEngine.SceneManagement;" (this allows us to utilize some of Unity's scene handling functions) This image is missing
    4. Line 4 should display the MonoBehavior class ‐ it is important to leave line 4 as is (without the MonoBehavior class, the script cannot be attached to an object and will not function) This image is missing
      • Notice that the name of MonoBehavior's class is the same as the name of the script file (required)
    5. Inside of MonoBehavior's curly brackets → {}, delete the default functions
      This image is missing This image is missing
    6. Inbetween these brackets (starting with line 6) we will add our own functions to change between scenes:
      1. Function to return to the start of the game after winning or losing:
        1. On line 6 (which should be an empty line between the curly brackets {}), write "public void GotoLevel1()" This image is missing
          • The function is public so that it can be accessed in the game
          • The function type is void as we don't need to return a value
          • The name of the function can be customized but must have the first letter in uppercase & not use any spaces (additional words can start with an uppercase letter)
          • The parentheses () are empty as we don't need to set any parameters
        2. After the right parenthesis ), type a space and then a left curly bracket { (the right bracket should automatically be added)
        3. Press enter (the brackets should automatically move down and leave an empty line inbetween) This image is missing
        4. On line 8, add "SceneManager.LoadScene("Level1");" This image is missing
          • We are utilizing Scene Management's SceneManager class
          • We are telling the SceneManager class to use its static method called LoadScene so that we can load a scene based on it's name or index
          • The parameters need to specify the name or index of whichever scene we want to load (if you are using a different scene name, use that instead of Level1)
      2. Function to load the lose scene:
        1. At the end of the first function (this should be line 9), click to the right of the curly bracket and press enter to begin a new line This image is missing
        2. Copy (Cmd+C) your first function and paste it (Cmd+V) into your empty line 10 This image is missing
        3. Change the function name to "GotoLose"
        4. Change the variable in the data block to the name of your lose scene (ie. Lose) This image is missing
      3. Function to load the win scene:
        1. At the end of the second function (this should be line 13), click to the right of the curly bracket and press enter to begin a new line
        2. Copy (comd+C) one of the functions and paste it (comd+V) into your empty line 14
        3. Change the function name to "GotoWin"
        4. Change the variable in the data block to the name of your win scene (ie. Win)
        5. This image is missing
      4. Lets add comments as reminders of what the code does (this is always a good practice):
        1. Click in front of "public" at the start of your first function (line 6) and press enter to move the function down a line
        2. Back in line 6, type // and add a descriptive comment
        3. Click in front of "public" at the start of your second function (line 11) and press enter to move the function down a line
        4. Back in line 11, type // and add a descriptive comment
        5. Click in front of "public" at the start of your third function (line 16) and press enter to move the function down a line
        6. Back in line 16, type // and add a descriptive comment
        This image is missing
      5. Save the script in Visual Studio (cmd+S)
      6. At this point the script is finished - at least for now. You can come back and make changes/additions as necessary at any time. Let's make sure the code is functioning back in Unity!
  1. Back in Unity, in the "Lose" scene, create a "Game controller" to connect the script to:
    1. In the Hierarchy window, "Create → Empty" (like in Blender, empty's are not visible in the game) This image is missing
    2. Drag the empty "GameObject" into the "Canvas"
      This image is missingThis image is missing
    3. Rename the empty from "GameObject" to "GameController" This image is missing
    4. Drag the script from the Project window over the Game Controller in the Hierarchy then release, to add the script to the controller This image is missing
      The script should now show at the bottom of the GameObject in the Inspector This image is missing
    5. Connect the Game Controller to the button in your lose scene:
      1. With the lose button object selected, in the Inspector, towards the bottom of "Button", click on the right + icon to add an on-click interaction This image is missing
      2. Change "Runtime only" to "Editor and Runtime"
        This image is missingThis image is missing
      3. Below, click on the circle next to "None" and in the Scene tab, select the Game Control empty This image is missing
      4. Change "No Function" to select the "Level1" function (ChangeScene → GoToLevel1) This image is missing
  2. Enter play mode and test the functionality - if there is a problem with the script there should be a notification at the bottom of the Unity Window
    1. Press the play button
    2. Once the game is loaded, try to trigger the effect you are testing - ie. press the "Play Again" button
    3. When ready, exit the game by pressing the play button again
    4. If the script did not function as expected, use the warning notification as a guide or use the troubleshooting steps in the sidebar to correct the script as necessary back in Visual Studio

Making and using Prefabs allows us to reuse groups of assets ‐ which we can then tweak as needed. This is a great way to save time.

  1. Make a Restart prefab to use in our Win Scene:
    1. Still in the Lose scene, in the Hierarchy, select the GameController and drag it over the canvas before releasing (this will group the controller with the button so that they can both be part of the Prefab)
    2. In the Projects window, make a Prefabs folder in the Assets folder
    3. Drag the entire Canvas from the Hierarchy into the Prefabs folder (now our restart Prefab is created)
    4. Rename the Prefab from "Canvas" to something like "Restart" This image is missing
    5. Also drag the "GameController" from the Hierarchy to the Prefabs folder
    6. Save the Lose scene with Cmd+S
    7. Load the "Win" scene (Projects window → Assets → Scenes → double-click on Win)
    8. Drag the "Restart" prefab from the prefab folder in the Projects window and drop it in the Hierarchy window This image is missing
    9. Customize for the Win Scene:
      1. Select the "Lose" text object and change the name and message in the Inspector This image is missing
      2. Change the background for the Main Camera from skybox to a custom color
    10. In the Hierarchy window, add an "EventSystem" if one is missing (+ → UI → EventSystem), so that the button will function
    11. Press the play button & test the game functionality
    12. Press Play again to exit and save the scene (Cmd+S)

Move on to the More tab before continuing on with scripting

Player Movement Script

This player movement script allows the up/down arrows and W/S keys to move the player forwards and backwards. Left/right arrows and A/D keys adjust the horizontal rotation of the player.

  1. In the Project window, select the Scripts folder
  2. Right-click in the Scripts window to create a new C# script
  3. Name the Script "PlayerMovement"
  4. Leave the first 6 lines as is
  5. Delete lines 7-11 and replace them with variables for movement speed and rotation speed (exact speeds can be increased/decreased later for more or less challenging gameplay) This image is missing
  6. Inside of the Update function, change the rotation with the following: This image is missing
    • This multiplies the the horizontal axis with the time since the last frame and the rotation speed to change rotation
  7. Still inside of the Update function, change the position with the following: This image is missing
    • This multiplies the the vertical axis with the time since the last frame and the movement speed to change position
  8. The final script will look like this: This image is missing
  9. Save the changes
  10. Back in Unity, drag the script from the Project Window over the Player object in the Hierarchy Window and release (the script should display in the Inspector when the player is selected)
  11. Play the game to test the script using WASD or arrow keys to move the player
  12. Exit the game and adjust the script variables if needed in Visual Studio

Win/Lose Script Adjustments

The player can win the game by reaching the goal in time. The player can lose the game by running out of time or by falling off of the ground plane.

The ChangeScene script already has the basic setup to trigger the win scene when the player object reaches the capsule (the goal). It needs to be adjusted to use a trigger. A trigger function can only be used once in each script.

  1. Open the ChangeScene script in Visual Studio:
    1. Edit Line 16 for the win script function:
      1. Delete "public"
      2. Replace "GotoWin()" with "OnTriggerEnter(Collider WinSensor)" This image is missing
        • When the collider for WinSensor is triggered by a rigidbody object, then the win scene loads
        • In the function parameters, WinSensor could be replaced by "other" or "Capsule" & the code would function the same
    2. Save the changes
    3. Back in Unity, drag the "ChangeScene script from the Project window over the Player object and release (the script shoild display in the Inspector when the player object is selected)
    4. Add a trigger to the Capsule:
      1. Select the capsule in the Hierarchy
      2. In the Inspector, check "Is Trigger" under "Capsule Collider" This image is missing
    5. Temporarily hide the obstacles by unchecking the the Obstacles empty in the Inspector when the empty is selected (this will make testing the win functionality go faster) This image is missing
    6. Press the play button to play the game and make sure your player can trigger the win scene by touching the WinSensor capsule This image is missing
    7. Press play again to exit the game
    8. Save any changes

Collisions between a rigidbody object and various colliders can also be used to trigger a script. The script does need to be attached to one of the collision objects. Usually the script should specify what is colliding or the function could be triggered unintentionally.

  1. Open the ChangeScene Script in Visual Studio
  2. On line 12, delete "public"
  3. Change "GotoLose()" to use "OnCollisionEnter" This image is missing
  4. Add an "If statement" so that the only collision that loads the lose scene happens when the player collides with the FallSensor - otherwise other collisions could load the lose scene too This image is missing
  5. The current ChangeScene.cs script should look like this: This image is missing
  6. Save the changes and return to Unity
  7. Since the ChangeScene script is already attached to the player, it doesn't need to be placed anywhere else
  8. Play the game and make the player fall off the ground to make sure the lose scene is loaded
  9. If you want a more dramatic fall before the lose scene is loaded, lower the Y value for the FallSensor plane
  1. In the Project window, select the Scripts folder
  2. Right-click in the Scripts window to create a new C# script
  3. Name the Script "TimerScript"
  4. Double-click on the script to open it in Visual Studio
  5. Follow the directions at this tutorial by John at gamedevbegninner.com to modify this script to make a Timer
  6. Save the changes to the script
  7. In the Level1 scene, connect the script to your timer text:
    1. In the Hierarchy window, expand the Canvas to show the Timer text object and the GameController object This image is missing
    2. Drag the TimerScript from the scripts folder of the Project window and drop it over the GameController object in the Hierarchy window
    3. In the Hierarchy window, select the GameController object
    4. In the Inspector window, scroll down to find the Timer Script component
    5. Next to "Timer Text", click on the circle and select the "Timer" text object This image is missing
    6. Play the game to make sure the timer works
    7. The amount of time can be adjusted in the "Time Remaining" field in the Inspector
  8. Save the scene

We can access data from one script within another script using GetComponent when the scripts are attached to the same object. Scripts can be accessed from other objects as well, though it is written differently.

  1. Open the "ChangeScene" script
  2. Add references to the timerscript and the scene names before the function to load "Level 1":
    • For the script, type: private, the name of the script, and the new name to reference the script, with a semi-colon at the end
    • For the scenes, type: private string SceneName;
    This image is missing
  3. After the new references, add an awake function to get the script and find the current scene: This image is missing
  4. After the awake function, add an update function to load the lose scene when level 1 runs out of time: This image is missing
  5. Save the changes to the script
  6. In Unity, play the game and make sure the lose scene loads when time runs out - it might be helpful to temporarily lower the "Time Remaining" value in the Inspector

Placeholder Prefabs

We will add some objects created directly by Unity to get our game play functionality working. If we make any placeholder objects into prefabs, we can easily replace the mesh components with original objects exported from Blender at a later time.

For the game, our player obect will navigate a ground plane with obstacles to reach a goal. Falling off the ground plane will hit a second plane which will act as a sensor & trigger the lose scene.

  1. Select the "Main Camera" in the Hierarchy and in the Inspector, set the position values to:
    • X: 0
    • Y: 1
    • Z: -52
  2. Back in the Hierarchy window, click on the "+" icon to add each new object for the game
  3. Add a plane for the ground: This image is missing
    1. In the Inspector, rename the plane as "Ground"
    2. Set the scale to X: 10, Y: 1, Z: 10
    3. Set the position to X: 0, Y: 0, Z: 0
    4. If this is just a placeholder and will be replaced, drag the object from the Hierarchy window into the Prefabs folder in the Project window
    This image is missing This image is missing
  4. Add a second plane for a "fall sensor": This image is missing
    1. In the Inspector, rename the plane as "FallSensor"
    2. Set the scale to X: 15, Y: 1, Z: 15
    3. Set the position to X: 0, Y: -5, Z: 0
    This image is missing This image is missing
  5. Add a cube for the player: This image is missing
    1. In the Inspector, rename the cube as "Player"
    2. Set the scale to X: 2, Y: 2, Z: 2
    3. Set the position to X: 0, Y: 1, Z: -48
    4. Drag the object from the Hierarchy window into the Prefabs folder in the Project window so that the mesh object can be easily replaced later
    This image is missing This image is missing
  6. Add a capsule for the "win sensor": This image is missing
    1. In the Inspector, rename the plane as "WinSensor"
    2. Set the scale to X: 1, Y: 1, Z: 1
    3. Set the position to X: 0, Y: 1.1, Z: 48
    4. Drag the object from the Hierarchy window into the Prefabs folder in the Project window so that the mesh object can be easily replaced later
    This image is missing This image is missing
  7. Add additional cubes (or etc.) as obstacles:
    1. Create the first obstacle, name it descriptively
    2. Customize the size/position (setting the starting position to X:0, Y:1, Z:0 will place the cube in the center of the plane - adjust from there as needed)
    3. Drag the first obstacle from the Hierarchy window into the Prefabs folder in the Project window so that it can be reused and the mesh object can be easily replaced later
    4. Duplicate the prefab multiple times in the Hierarchy window to save time (Right-click → Duplicate)
    5. Scale & position as needed
    This image is missing
  8. Add an empty as a parent for the obstacles: This image is missing
    1. In the Inspector, rename the empty as "Obstacles"
    2. In the Hierarchy window, drag each obstacle slightly to the right of the empty object to place
    This image is missing
  9. Make the camera a child of the player (so that the camera will follow the player):
    1. In the Hierarchy window, drag the Main Camera slightly to the right of the Player This image is missing
    2. Adjust the camera's position if needed

Materials

  1. In the Assets folder, create a "Materials" folder
  2. Open the Materials folder in the project window and right-click in the background to choose "Create" → "Material" Image is missing
  3. Give the material a descriptive name and press enter Image is missing
  4. With the new material selected, in the Inspector, use the color picker to choose a color
  5. Adjust other settings as needed - ie. Rendering Mode, Metallic, Smootness &etc;... Image is missing
  6. Add the material to an object by dragging the material from the Assets folder and releasing it over the object in the Inspector

Physics

Like Blender, different types of physics can be utilized in Unity. We'll be focusing on the 3D physics options

  • Rigidbody - similar to Blender, Rigidbody is dynamic & allows gravity to impact an object
  • Colliders - colliders form a mesh outline of various shapes around an object (just like Blender's rigid body collision shapes) so that the rigidbody objects can interact/collide with the colliders; without rigidbody, colliders are considered static
  • Joints - can connect rigid bodies to each other or to a fixed point; forces can be applied & movement restricted to specific ranges
  • Character Controller - used to control characters so that they can interact with rigidbodies but won't fall through walls/floors; this uses a capsule collider & keeps the character upright (this controller is not limited by realistic behavior)
  1. Add "Rigidbody" to the Player object:
    1. Select the player object in the Hierarchy window
    2. In the Inspector, select "Add Component" at the bottom This image is missing
    3. Under "Physics", select "Rigidbody"
      This image is missing This image is missing
    4. Since gravity is checked, the rigidbody can now fall when playing the level:
      1. Press play This image is missing
      2. In the Hierarchy, select the player object
      3. In the Inspector, set the Y position to 10
      4. After the player falls, press play to exit (notice the Y value returns to the original value since changes made in Play mode are temporary)
  2. For now, leave the default collider types for the various objects
    • Obstacles & player = box collider
    • Ground plane & Fall Sensor = mesh collider, set to plane
    • Win sensor = capsule collider

After adding physics, return back to the "Scripting" tab to write scripts to move the player object and win or lose the game.

Before trying to use Blender assets in a Unity game:

  1. Meshes should be formed with quads or triangles
  2. Make sure in the front view (numpad 1), the object is facing front
  3. Apply all modifiers
  4. Apply all transformations (Ctrl+A)
  5. Mesh & origin are centered in the grid:
    1. Select the mesh in "Object" mode
    2. Shift+S → "Cursor to World Origin"
    3. Shift+S → "Selection to Cursor"
    4. If needed, in the "Object" menu, choose "Set Origin" → "Geometry to Origin"
    5. In some cases, it might be best to slide up the mesh (in Edit mode) so that the origin is at the base of the mesh
  6. Unwrap the mesh to create a UV map - if you will be using Blender materials/textures:
    1. In Edit mode, select the entire mesh (A)
    2. Press "U" to unwrap and choose "Smart Project"

There are 2 ways to use Blender assets in Unity. Blender objects can be exported as .fbx files - this is usually recommended especially when working with a team as it doesn't rely on having Blender installed. Alternatively, Blender files can be used directly - which allows for easy updates if the asset changes but may slow down Unity.

FBX files are a standard 3d asset file format. They can be exported from Blender for use in a variety of programs, including Unity.

  1. In the "File" menu, go to "Export" → "FBX (.fbx)"
  2. Choose what will be exported as an FBX:
    1. Everything in the file, including cameras and lights (default behavior)
    2. The selected objects (check "Selected Objects")
    3. The active collection (check "Active Collection")
    4. Or control the type of data to export by enabling or disabling the different "Object Types"
  3. Give the file a name
  4. Choose the Unity project's asset folder as the export location
  5. Check "Apply Transform" under "Transform" (you may need to scroll down for this option to be visible)
  6. Press "Export FBX"
  7. In Unity, Find the FBX file in the Project window's Asset folder
  8. Drag the FBX file from the Project Window's Asset Folder into the "Hierarchy Window" and release when hovering over the placeholder object to replace it (this keeps any components from the placeholder)
    • To replace the object without keeping the components, hold Opt | Alt while dragging
  1. Move your .blend file into the Assets folder of your project (using Finder, File Explorer or etc.)
  2. In Unity, Find the FBX file in the Project window's Asset folder
  3. Drag the FBX file from the Project Window's Asset Folder into the "Hierarchy Window" and release when hovering over the placeholder object to replace it (this keeps any components from the placeholder)
    • To replace the object without keeping the components, hold Opt | Alt while dragging

After adding your Blend file or FBX file in Unity, you may need to make adjustments to the scale, physics, and materials.

Blender Physics do not currently transfer to Unity - no matter what way you import your assets to Unity. For certain behaviors, you can bake/apply the physics (possibly converting to mesh) as a partial workaround. Otherwise, create the physics in Unity instead.

There are some adjustments that should be made in Unity to the Physics after replacing your placeholder objects.

  1. Select the replaced object in the Hierarchy
  2. In the Inspector, find the collider component and click on the "Edit" button Image is missing
  3. Edit the collider to fit the object, checking the fit from multiple views:
    • Click+drag the green squares to adjust the size/position of the collider Image is missing
    • Drag on the collider's labels to resize or position the collider differently Image is missing
    • Experiment with different collider types if needed
  4. Adjust the player's rigidbody settings:
    1. Stop the player from passing through parts of the obstacles by changing the "Collision Detection" to "Continuous Dynamic"
    2. Stop the player from falling over when hitting obstacles by adding constraints to the rotation on the "X" and "Y" axi
    Image is missing

Materials don't automatically transfer into Unity. For basic materials, make them directly in Unity instead. Directions can be found on the More tab.

Cycles Materials can be imported into Unity from Blender - but only if they've been baked onto an image.

  1. Create a U/V map of the mesh (if one hasn't been created already)
    1. In Edit mode, select the entire mesh with "A"
    2. Press "U" and choose one of the unwrap optons (Smart UV Project is recommended)
    3. In the "UV Editing" workspace, adjust the map
  2. Create an image to bake the material onto:
    1. In the "UV Editing" workspace, click on "+ New" at the top of the "UV Editor" panel
    2. Give the image a name and choose "OK"
    3. Save the image using the "Image" menu → "Save" at the top of the UV Editor (save the image directly into the Assets/Materials folder for the game project)
  3. "Connect" the image to each material:
    1. In the "Shading" workspace, add an image texture node to the active material (Shift+A → Texture → Image Texture)
    2. Place the node to the side (not directly connected to the material nodes)
    3. Open the blank, saved image
    4. In the "Materials" tab of the properties panel, switch to the next material & repeat the previous steps
  4. Bake the materials onto the image:
    1. In the properties panel, switch to the "Render" tab of the properties panel, set the render engine to "Cycles"
    2. In the "World" tab and check "Ambient Occlusion" to avoid overly strong shadows
    3. Back in the "Render" tab, expand the "Bake" subsection and click on "Bake"
  5. Save the changes to the baked material in the "UV Editor" panel (Image → Save)
  6. If necessary, move the baked material image into the Assets/Materials folder of the Unity project with Finder or File Explorer
  7. In Unity, create a new material for the image texture in the Project panel (Create → Material)
  8. Drag the image from the Project panel and drop it over the material's "Albedo" field in the Inspector (the material should display the baked image in the material preview icon below)
  9. Drag the material from the Project panel over the desired object in the Hierarchy panel

U/V maps automatically export as part of a mesh object in an FBX file. As long as the mesh object was exported from Blender with a U/V map already made, the material texture should apply perfectly in Unity. If the U/V map was created after, an FBX file should be exported again.

The process of creating/exporting a game app is called building. There are some settings that should be set before building the playable game app.

  1. In The File menu, go to "Build Settings": Image is missing
    1. Select the operating system that you are building the game for:
      • macOS for this class's game submission
      • Optionally build the game for other OS, if needed for home use/sharing with friends
    2. Open the "Player Settings..."
    3. Under "Player" → "Resolution and Presentation", change from "Fullscreen Window" to "Maximized Window" (this allows the game to be minimized and closed without additional scripting) Image is missing
  2. Build the game directly from the project settings window, or from the File menu

Before building the game, consider if there are missing features you would like to incporporate - some possible additions might include:

  • Custom images for an app icon and/or splash screen (these can also be set in the Player settings)
  • A menu or etc. upon opening
  • A "Quit" action (start with the following code - the quit function could be accessed by a button...) Image is missing
  • Additional levels

Have fun customizing the game & making it your own! When you do build the final game, make sure to test out the app in the macLab to make sure it functions properly.