Unity Workshop 1
Last Updated: 09/19/2021 Tested With: Unity 2020.3.18f1 LTS
Useful Things: GitHub Student Developer Pack Unity Student Plan
Unity Stuff: Unity Manual Unity Scripting API
Programming Software (Download One of These!):
- VS Code
- Visual Studio
- Atom
- Notepad++ (possible, but why would you do this)
- Rider for Unity (I don’t know anything about this one yet, I just found out about it recently)
Downloading the Software
- Download and install Unity Hub
- Go to
Installs
>Install Editor
and download whatever version you want to try out- I recommend downloading the lastest LTS version
- Use an LTS version if you are planning on working on the same project for a while
- Use a Beta version if you want to try out the latest features
- Select the modules that you want (any platforms that you want to be able to build your project on) and hit install
- Note: Only machines running MacOS can actually build for iOS devices
(Optional) GitHub Setup
- Download Git
- Create an account on GitHub if you haven’t already
- Press the + button and create a new repository
- Name the repository whatever you want your project to be called
- Initialize it with a README.md
- https://choosealicense.com
- In your repository, click the
Code
button and copy the link - Go into your command prompt / termial and type
git clone [url]
Setting Up Your Project
- Click
Projects
>New Project
- Make sure the
Editor Version
at the top is the one you want to work with if you have multiple installs of Unity - Select
Core
>3D
- In the rightmost panel, set your project name and file location
- GitHub step: make sure you save it in your repository folder
- Press the
Create Project
button
Unity Interface
Unity interface has many windows to start off with:
- Scene - where you construct your game
- Game - how the game will actually look
- Hierarchy - where all the GameObjects are stored in a list (using inheritence)
- Inspector - where any extra info about the selected item will pop up
- Project - project files
- Console - code logs
Package Manager & Importing Assets
- Download Dog Knight
- Add the character model to your assets in the online Asset Store
- You will need a Unity account for this
- You can press “Open in Unity” if prompted and it will automatically open the
Package Manager
- Back in Unity, go to
Window
>Package Manager
- Where you can download and install assets and add-ons
- On the top, next to the +, change the sorting method from
in project
tomy assets
- Click the sign in button if you can’t see anyhting
- Select the character model and press the import button in the bottom right of the window
- This next popup lets you select what specific asset files we want to import
- Just leave it with all of them selected and press import
Making a Floor
- Create the object
- Press the + button in the hierarchy
- Select
3D object
>Cube
- Name it “Floor”
- Position and scaling
- Leave placed at 0, -0.1, 0 (y pos should be half the height of the object)
- Scale 20, 0.2, 20 (or really whatever you want)
Making a Character
Putting the Model In Game
- Navigate to the
Prefabs
folder of the asset you just downloaded - Drag one into the scene or hierarchy
- Right-click in hierarchy and select
prefab
>unpack
- Select it in the hierarchy
- In the inspector, all of the info should show up
- Change name to player
- Press the 3 little dots in the corner of the
transform
component and selectreset
to make sure it’s in the center of the scene - Disable the
animator
component for now (little checkbox)
Adding Components
- Add capsule collider
- Adjust the values to fit your model
- For me: center = 0, 0.75, 0 and height = 1.5
- Constraints: freeze rotation x & z
- Add rigidbody
Making it Move
Setting Up the Input System
Using this because it’s much easier to work with and a lot more extensible (really nice for multi-platform development)
- Go into package manager, change sorting to Unity registry
- Install input system
- If you get a warning that says the following, press yes
This project is using the new input system package but the native platform backends for the new input system are not enabled in the player settings. This means that no input from native devices will come through.
Do you want to enable the backends? Doing so will RESTART the editor and will DISABLE the old UnityEngine.Input APIs.
We’re going to set up input through Input Actions because it’s much easier to modify and handle if we are using multiple input devices (although you can still add through code) refer to the quick start guide to see both approaches
- Add a
PlayerInput
component to your character - Press the “Create Actions” button
- Save in the Assets folder and name it whatever (I picked “GameActions”)
- Press save
- An error may pop up, we can ignore it (can also go to console and clear it)
- Drag the newly created .inputactions file onto the
Actions
slot in thePlayerInput
component - Double-click on the .inputactions file to open the editor window
- The defaults should be all we need, but feel free to look around and change things if you want different key bindings
- Close when you’re done
- Change
Behavior
to “Invoke Unity Events” - Expand the events tab and the player tab, we will need this soon
Making a Script
- Navigate back to the root folder (called Assets) in the project window
- Make a new folder named “Scripts”
- Can press the + button or right click
- Unity special folder names
- Make a new C# script in this folder called “PlayerController”
- Double click the script to open it
- Add
using UnityEngine.InputSystem
to the top of the file - Order of execution for event functions
public void Move(InputAction.CallbackContext context) {
Debug.Log("Moving!");
}
Warning!
If the script name and the class name don’t match, the script will not work. If you’re having issues, check to make sure the script’s name in Unity matches the
public class ClassName : MonoBehaviour
whereClassName
is the name of the script.This often happens when a script is created and renamed later.
Adding the Script to the Game
- Go back into editor and drag script onto player
- In the
PlayerInput
component, press the + under Move - Drag the Player game object into the slot that appears
- In the fucntion dropdown, select
PlayerController
>Move
- Open the console and press the play button, try pressing the movement controls and watch it print “Moving!”
Actually Making it Move
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
// the class that controls the player
public class PlayerController : MonoBehaviour {
[SerializeField]
private float _speed = 5.0f;
private Vector3 _direction;
// called before the first frame update
void Start() {
}
// called once per frame
void Update() {
// move the player's transform using the direction vector
// and speed (Time.deltaTime keeps this constant)
transform.Translate(_direction * (_speed * Time.deltaTime));
}
// to be called when the move event is triggered in PlayerInput
public void Move(InputAction.CallbackContext context) {
// calculate direction vector (x, y, z) using PlayerInput
// movement values
_direction = new Vector3(context.ReadValue<Vector2>().x,
0,
context.ReadValue<Vector2>().y);
}
}
[SerializeField]
makes private variables visible in the inspector
Camera
- Drag the main camera onto the player to make it a child of the player
- Follows the player
- Children of an object inherit position and rotation - bad if the player rotates or something
- Better to make a script to follow the player
- Drag the main camera out of the player
- Make a new script called “CameraController”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
private PlayerController player;
private Vector3 offset;
// called before the first frame update
void Start() {
// finds the class PlayerController in the scene
player = FindObjectOfType<PlayerController>();
// create an offset vector (distance from the camera)
offset = transform.position - player.transform.position;
}
// called once per frame (after all Update()s have been called)
void LateUpdate() {
// update the camera position to the players position
// plus the offset (to keep at constant following distance)
transform.position = player.transform.position + offset;
}
}
- Drag camera controller script onto the main camera
- Adjust camera transform values to your liking
- Mine is pos: <0, 3, -5> rotation: <30, 0, 0>