Project: 'Delivery Driver'

DrewDev·2023년 8월 14일
0
post-thumbnail

Gameplay Design


Gameplay Overview Screen

Game Mechanics

  • Driving car forwards and backwards
  • Turning car left and right
  • Increase speed up when drive over 'Speed Ups'
  • Decrease speed when bump into 'Slow Downs'
  • Pick up packages when drive over them
  • Deliver package when drive over delivery spot
  • Change car color to show status

Game Design

  • Player Experience: Relaxing
  • Core Mechanic: Drive over pickups
  • Game Loop: Find and deliver all the packages to win

 

Methods


What are Methods?

  • Methods (also called Functions) execute blocks of code that makes our game do things.
  • We can use the methods already available in Unity or create our own methods.

Creating and Calling

  • When we CREATE a methods, we are giving it a name and saying what it should do.
  • When we CALL a method we are saying "do the things now please".

 

Default Script


The using keyword provides packages for us to use for free while building our project.

Methods are stored in classes and then called with a callback function.

void Start is called when we start the game.

void Update is called for every single frame of the game (after the game is started).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Driver : MonoBehavior
{
	// Start is called before the first frame update
    void Start()
    {
    
    }
    
    // Update is called once per frame
    void Update()
    {
    
    }
}

 

Transform Methods


transform.Rotate()

Rotates the game object to a certain position once.

// Start is called before the first frame update
void Start()
{
	transform.Rotate(0, 0, 45);
}

Will continue to rotate every frame per second.

(If the number is a decimal, put the f keyword at the end for float).

// Update is called once per frame
void Update()
{
	transform.Rotate(0, 0, 0.1f);
}

 

transform.Translate()

Moves the game object either forward or backward.

// Update is called once per frame
void Update()
{
	transform.Translate(0, 0.01f, 0);
}

Combining both rotate and translate method allows the game object to move forward/backward while rotating at the same time.

// Update is called once per frame
void Update()
{
	transform.Rotate(0, 0, 0.1f);
	transform.Translate(0, 0.01f, 0);
}

 

Variables


Variables Are Like Boxes

  • Variables help us store, manipulate and refer to information.
  • Each variable has a NAME.
  • Each variable containes DATA.
  • Each variable is of a particular TYPE
// Example
int hitPoints = 20; // int refers to numbers

Common Types of Variables

  • int - whole numbers
  • float - fractional numbers (up to 6 decimal places)
  • double - fractional numbers (up to 15 decimal places)
  • bool - true or false
  • string - sequence of characters (with quotatino marks)
float speed = 3.8f; // write 'f' at the end for floats
bool isFun = true;
string myName = John;

Using Variables

Write variables within classes and outside the Start() and Update() functions.

public class Driver : MonoBehavior
{
	float steerSpeed = 0.1f;
    float moveSpeed = 0.01f;
    
    void Start()
    {
    
    }
    
    void Update()
    {
    	transform.Rotate(0, 0, steerSpeed);
        transform.Translate(0, moveSpeed, 0);
    }
}

 

SerializeField


SerializeField is an attribute that you can use to force a private field to be visible in the Unity Editor's Inspector without making it public. This way, you can still adhere to good object-oriented practices by keeping fields private, while still being able to set their values from the Editor.

Using SerializeField allows us to directly test our code on the Unity Editor visually.

[SerializeField]
// Example
public class Driver : MonoBehavior
{
	[SerializeField] float steerSpeed = 0.1f;
    [SerializeField] float moveSpeed = 0.01f;
    
    void Start()
    {
    
    }
    
    void Update()
    {
    	transform.Rotate(0, 0, steerSpeed);
        transform.Translate(0, moveSpeed, 0);
    }
}

 

Input System


Unity Input System

  • Input System: converting the the player's physical action (ex. button press, key press) into information fo rthe game.
  • Unity has used a few different input systems.
  • Currently there is an "old" system and a "new" system.
  • The "old" system is used in this project.

Input.GetAxis()

To implement an input system, we need to use the Input class and select the type of input system we want to use (ex. keyboard/mouse, joystick).

We put our input variable within the update method because the amount of frame needs to be updated constantly.

If you are using the "old" system, make sure to specifically use the exact names.

void Update()
    {
        float steerAmount = Input.GetAxis("Horizontal") * steerSpeed;
        float moveAmount = Input.GetAxis("Vertical") * moveSpeed;
        transform.Rotate(0, 0, -steerAmount);
        transform.Translate(0, moveAmount, 0);
    }

Time.deltaTime()


Depending on the specs of our computer, our game object can move much faster or slower.

Using Time.deltaTime()

  • Using Time.deltaTime, Unity can tell us how long each frame took to execute.
  • When we multiply somthing by Time.deltaTime it makes our game "frame rate independent".
  • In other words, the game behaves the same on fast and slow computers.

We simply need to multiply our speed by the **Time.deltaTime

void Update()
    {
        float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;
        float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        transform.Rotate(0, 0, -steerAmount);
        transform.Translate(0, moveAmount, 0);
    }

 

Colliders & Rigidbodies


Colliders are components attached to GameObjects in Unity that define their physical shape for the purposes of collision detection and physics interactions. They are used to determine whether two objects are touching or intersecting in the game world. Colliders come in various shapes, such as boxes, spheres, capsules, and more complex mesh-based colliders.

Unity provides several types of colliders, including:

  • Box Collider: Represents a rectangular box-shaped collider.
  • Sphere Collider: Represents a spherical collider.
  • Capsule Collider: Represents a capsule-shaped collider, which is like a cylinder with rounded ends.
  • Mesh Collider: Represents a collider based on the actual geometry of a mesh.

Rigidbodies are components that are used to simulate physical behavior and movement in Unity's physics system. When you attach a Rigidbody component to a GameObject, that GameObject becomes subject to Unity's physics simulations, including forces, gravity, and collisions. Rigidbodies allow objects to be affected by physics forces like gravity, be pushed or pulled by other objects, and react realistically to collisions.

Rigidbodies have various properties and settings that can be adjusted, such as mass, drag, angular drag, and constraints on movement and rotation. By manipulating these properties and applying forces or torque to rigidbodies, you can create a wide range of dynamic and interactive behaviors within your game.

 

OnCollisionEnter2D()

OnCollisionEnter2D() is a method in the Unity game engine that is used in 2D physics-based games to detect collisions between two game objects with Collider2D components.

With this method, 2D sprites will react as soon as they come into contact with another game object.

// Example: The game object reacts with a message
public class Collision : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D other) 
    {
        Debug.Log("Ouch!");
    }
}

 

OnTriggerEnter2D()


OnTriggerEnter2D is a method in Unity's MonoBehaviour class that gets called when a 2D collider enters a trigger collider. It's commonly used in 2D games to detect when two colliders intersect in a way that is set up as a trigger interaction.

Ex. Power ups in Super Mario Bros., hearts in the Legend of Zelda.

void OnTriggerEnter2D(Collider2D other) 
    {
        Debug.Log("What was that?!?");
    }

 

Assets


Basic Knowledge

In Unity, assets refer to the various types of files and resources that are used to create and build your game or interactive experience. Assets are the building blocks of your Unity project, and they can encompass a wide range of elements, including:

  • 3D Models: These are representations of objects in a 3D space. They can include characters, props, environments, and more.

  • 2D Sprites and Textures: These are images used for characters, backgrounds, user interface elements, and more.

  • Animations: Animation assets contain keyframes and data for creating dynamic movement and behavior for characters and objects.

  • Audio Clips: These assets store audio data for sound effects, music, voiceovers, and other auditory elements.

  • Scripts: Unity uses scripts written in languages like C# to control the behavior and functionality of objects in your game.

  • Materials and Shaders: Materials define how an object's surface should appear, and shaders control how light interacts with those materials.

  • Prefabs: Prefabs are templates for GameObjects that can be reused throughout your project. They allow you to create instances of a GameObject with predefined properties and components.

  • Scenes: Scenes are individual levels or sections of your game. They consist of a collection of GameObjects, assets, and other settings that define a specific part of your game.

  • Particle Systems: Particle systems create dynamic effects like smoke, fire, explosions, and more.

  • Physics Materials: These materials define how objects interact with each other physically, such as friction and bounciness.

  • Animations: Animation assets are used to create animations for characters, objects, and other elements in your game.

  • Fonts: Font assets define the appearance of text in your game's user interface.

  • Terrain Data: If your game uses terrains, this asset type contains heightmaps and other data that define the landscape.

  • Video Files: Video assets can be used for cutscenes or interactive video elements in your project.

  • Assets are organized within the Unity project's Assets folder and can be imported, managed, and manipulated through the Unity Editor. Unity provides a wide range of tools to work with these assets, allowing developers and designers to create complex and engaging interactive experiences.

 

Sprites Are Made of Pixels

  • Resolution refers to the number of pixels in an image.

  • Higher resolution = more pixels

 

Unity Units

  • 1 Unity Unit has no meaning, just whatever we want it to represent. It could be meters, kilometers, miles, inches, whatever we want.

 

Pixels Per Unit

  • New assets default to 100 pixels per Unity unit.

  • Bigger: less pixels per unit


profile
I LOVE GAMES

0개의 댓글