Workshop 2: Critical Thinking in Game Development

Introduction to Unity Editor & C# Scripting

Lucas P. Cordova, Ph.D.

Willamette University

Game Development Skills

Core Competencies

  • Learning - Continuous adaptation
  • Critical Thinking - Design decisions
  • Problem Solving - Debug & iterate
  • Mathematics - Probability & functions
  • Programming - Unity/C# mastery
  • Creativity - Unique experiences

Game Development Competencies

Unity Roadmap 🗺️

Where We’re Going in Unity

  • Weeks 2-5: 2D Game Development
    • Sprites, Physics2D, Tilemaps
    • 2D Game Mini-Project
  • Weeks 6-10: 3D Game Development
    • 3D Models, Lighting, Physics
    • Advanced Interactions
    • 3D Game Mini-Project
  • Final Project: Your Choice!
    • Any Unity game or simulation
    • 2D, 3D, or hybrid approach

Unity Editor Interface

Version Compatibility ⚠️

  • Unity Editor interface varies by version
  • Best Practice: Match tutorial version exactly
  • If versions differ:
    • Expect UI differences
    • Research feature locations
    • Check Unity documentation

You should have completed the Editor Interface tutorial

Layout Best Practices

  1. Window → Layouts → Tall
  2. Move Game View below Scene View
  3. Enable Console (Window → General → Console)
  4. Arrange for maximum workspace

Unity Tall Layout

Benefits of Tall Layout

Scene View

  • Maximum vertical space
  • Better for level design
  • Easier object placement

Game View + Console

  • Test gameplay immediately
  • See debug output live
  • Catch errors quickly

Today’s Demo: Bearcat Wheel of Fate 🎯

What We’ll Build

  • Interactive spinning wheel game
  • Introduction to C# scripting
  • Basic Unity 2D setup
  • Input handling with new Input System

Bearcat Wheel of Fate

Project Setup

Creating a New 2D Project

  1. Unity Hub → New Project
  2. Select 2D (URP) template
  3. Name: “BearkatWheelOfFate”
  4. Choose location → Create

Import Assets

  • Wheel sprite (provided)
  • Marker/pointer sprite (provided)
  • Drag into Assets folder

Scene Setup

Positioning GameObjects

  1. Wheel GameObject
    • Position: (0, 0, 0)
    • Scale as needed
  2. Marker GameObject
    • Position above wheel
    • Ensure proper sorting order

Scene Setup

Introduction to C# Scripting

Creating Our First Script

  1. Assets → Create → C# Script
  2. Name: WheelSpin
  3. Attach to Wheel GameObject
  4. Open in code editor

The WheelSpin Script

using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class WheelSpin : MonoBehaviour
{
    private float _rotationSpeed = 0.0f;
    
    // Start is called once before the first execution of Update
    void Start()
    {
        _rotationSpeed = 0.0f;
    }
    
    // Update is called once per frame
    void Update()
    {
        // Check if the space bar was pressed this frame
        if (Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            _rotationSpeed = new System.Random().Next(2000, 5000);
        }
        
        if (_rotationSpeed > 0)
        {
            float rotationAmount = _rotationSpeed * Time.deltaTime;
            transform.Rotate(0, 0, rotationAmount);
            
            // Decrease rotation speed over time
            _rotationSpeed -= 500 * Time.deltaTime;
        }
    }
}

Understanding the Script - Part 1

Key Components

  • using statements - Import Unity functionality
  • MonoBehaviour - Base class for Unity scripts
  • **private float _rotationSpeed**
    • Private: Only accessible within this class
    • float: Decimal number type
    • Underscore prefix: C# convention for private fields

Understanding the Script - Part 2

Unity Lifecycle Methods

void Start() - Called ONCE when GameObject becomes active - Initialization code goes here - Runs before first Update()

void Update() - Called EVERY frame (30-120+ times/second!) - Game logic and input checking - Performance critical code

Understanding the Script - Part 3

Transform & Rotation

transform.Rotate(0, 0, rotationAmount);
  • transform - Built-in reference to GameObject’s Transform
  • Controls position, rotation, scale
  • Rotate(x, y, z) - Rotates around each axis
  • Z-axis rotation for 2D spinning

Time.deltaTime Explained

Frame-Independent Movement

float rotationAmount = _rotationSpeed * Time.deltaTime;
  • Time.deltaTime = Time since last frame
  • Makes movement consistent across different framerates
  • 60 FPS: deltaTime ≈ 0.0167 seconds
  • 30 FPS: deltaTime ≈ 0.0333 seconds

Adding Debug Output

Debugging with Console

void Update()
{
    if (Keyboard.current.spaceKey.wasPressedThisFrame)
    {
        _rotationSpeed = new System.Random().Next(2000, 5000);
        Debug.Log($"Spinning! Initial speed: {_rotationSpeed}");
    }
    
    if (_rotationSpeed > 0)
    {
        // ... rotation code ...
        Debug.Log($"Current speed: {_rotationSpeed:F2}");
    }
}

Testing Your Game

Play Mode Testing

  1. Save your script (Ctrl/Cmd + S)
  2. Return to Unity Editor
  3. Press Play button ▶️
  4. Press Spacebar to spin
  5. Watch Console for debug messages

Wheel Segments Decoded 🎰

Your Fate Awaits…

  1. Bearcat Jackpot 🎉
    You landed big! (good fortune)

  2. Mill Stream Mishap 🦆
    Oops, you slipped by the ducks (bad luck)

  3. Star Trees Wish
    Make a lucky wish under the trees

Wheel Segments (continued)

  1. Waller Hall Haunt 👻
    Unlucky… you met a campus ghost

  2. Kaneko Lucky Break 🍕
    You found free snacks in the lounge

  3. Goudy Gamble 🍽️
    Mystery food choice, will it be lucky or not?

Input System Resources

Exploring Keyboard.current

// Other useful inputs:
Keyboard.current.enterKey.wasPressedThisFrame
Keyboard.current.escapeKey.isPressed
Keyboard.current.wKey.wasPressedThisFrame

Documentation

Key Takeaways

  • Unity Editor layout affects productivity
  • C# scripts bring GameObjects to life
  • Start() initializes, Update() runs game logic
  • Time.deltaTime ensures smooth movement
  • Debug.Log() is your debugging friend
  • Input System provides modern input handling

Next Lecture Preview

Gearing Up Towards the 2D Mini-Game Project 🎮

  • Sprite animations
  • Tilemap environments
  • Player movement & collision
  • Enemy AI basics
  • Collectibles & UI

Questions & Practice Time

Your Turn!

  1. Modify the spin speed range
  2. Add different input keys
  3. Experiment with rotation direction
  4. Add sound effects (stretch goal)

Remember

  • Save scripts before testing
  • Check Console for errors
  • Experiment and have fun!