0% found this document useful (0 votes)
30 views52 pages

Unity Certification Training Material

The document outlines a training course for Unity Certified User Programmer, covering key concepts such as the Unity interface, asset management, scripting with C#, and game development techniques. It includes practical examples of creating a 2D game, implementing physics, animations, UI elements, and sound effects. The material is structured to guide learners through the process of developing interactive simulations and games using Unity's features.

Uploaded by

orsika2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views52 pages

Unity Certification Training Material

The document outlines a training course for Unity Certified User Programmer, covering key concepts such as the Unity interface, asset management, scripting with C#, and game development techniques. It includes practical examples of creating a 2D game, implementing physics, animations, UI elements, and sound effects. The material is structured to guide learners through the process of developing interactive simulations and games using Unity's features.

Uploaded by

orsika2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Unity Bootcamp Training

Material
For Unity Certified User Programmer
Course

By Khan Azish Ahmed 1


Introduction
to Unity

• Unity is a versatile, real-


time development
platform.
• Supports 2D, 3D, AR,
and VR development.
• Cross-platform
deployment for PC,
Mobile, and Consoles.
• Ideal for creating games
and interactive
simulations.

By Khan Azish Ahmed 2


Unity Interface
Overview

• Scene View: Allows you to


visually navigate and edit your
Scene..
• Game View: Simulates your
project during runtime when
you press play button.
• Hierarchy Window: Manage
GameObjects placed in scene.
• Inspector Window: Allows
you to view and edit all the
properties of the currently
selected GameObject..
• Project Window: Organize
assets and resources.

By Khan Azish Ahmed 3


Assets Store Overview
A marketplace for free and commercial assets created by Unity and members of the community
https://assetstore.unity.com/

By Khan Azish Ahmed 4


Changing the IDE
Goto: Edit -> Preferences -> External Tools

By Khan Azish Ahmed 5


Changing Windows Layout

By Khan Azish Ahmed 6


UI Color changes

Goto: Edit -> Preferences -> Colors

By Khan Azish Ahmed 7


UNITY OVERVIEW

1. Everything you see in the scene and Hierarchy is a GameObject.

GameObject : GameObjects act as invisible container in which you can add


functional components which determine how it looks like and what it does.
8

2. Unity Uses C# for Scripts

Scripts : Scripts allow you to customize and extend the capabilities of your
applicaton with C# code. With scripts that derive from Unity’s built-
in MonoBehaviour class you can create your own custom Components to
control the behavior of GameObjects.
By Khan Azish Ahmed

3. Object-Oriented Programming (OOP) and Component-Based Software


Engineering (CBSE)
Setting up a 2D
environment.

Using the Sprite Renderer


and Sprite Editor.
Mini 2D
Game Adding physics to 2D
objects (Rigidbody2D,
BoxCollider2D).

Animation basics
(Animation Controller and
State Machines).
By Khan Azish Ahmed 9
Point to Ponder : When images are imported in
2D mode, Unity interprets them as 2D sprites
Mini 2D Game rather than textures that are intended to be
wrapped around a 3D object.

By Khan Azish Ahmed 10


Movement and Physics

1. Add RigidBody2D to the Player GameObject.


A Rigidbody2D is a 2D component you can use to enable an object to act
under the control of physics. Rigidbodies enable physics-based behavior, such
as reactions to gravity, mass, drag, and momentum.

2. Add Boxcollider2D to the Ground and Player


GameObjects.
The Box Collider2D component that interacts with the 2D physics system
for collision detection. The collider 2D defines which area of the GameObject
has collision
and can interact with other colliders in the scene
.

By Khan Azish Ahmed 11


Gravity Physics

By Khan Azish Ahmed 12


To add the script to
desired gameobject
go to the
components and
write the name you
want to give to the
script and then
select new script ->
Create and Add

By Khan Azish Ahmed 13


•Create a MonoBehaviour Script (This script enables Unity to
attach behaviors to GameObjects) in the assets folder.

•Awake() -> method is called when the script instance is being


MonoBehaviour loaded, even if the GameObject is inactive.

Script •Start() -> method is called before the first frame update, but
only if the GameObject is active

•Update() -> method is called once per frame. This is where you
implement behaviors that need to happen continuously

By Khan Azish Ahmed 14


Execution order of different
unity script methods

By Khan Azish Ahmed 15


Unity Scripts Overview
• Debug.Log
• Display in Inspector:
– public keyword
– [SerializeField] attribute
• Naming conventions:
– Variable: camelCase
– Method: PascalCase
• Access modifiers:
– private, public
• for encapsulation
By Khan Azish Ahmed 16
Unity Scripts Overview

Member Access (.):


•Access object members directly.
•Throws NullReferenceException if the object is null.
Example:
int value = obj.SomeProperty; // Throws exception if
obj is null

Null-Conditional (?.):
•Safely access members, returns null if the object is null.
Example:
• int? value = obj?.SomeProperty; // No exception, returns
null

By Khan Azish Ahmed 17


Unity Scripts Overview
Reading input
• Keyboard
– Input.GetKey(…)
– Input.GetKeyDown(…)
– Input.GetKeyUp(…)
– Input.GetAxis(…)
• Mouse
– Input.GetMouseButton(…)
– Input.GetMouseButtonDown(…)
– Input.GetMouseButtonUp(…)
By Khan Azish Ahmed 18
Player Controller Script
Add a public variable of type Rigidbody2D to contact with the Rigidbody2D
component of Player.
Actually we need it to access the physics features that are used to apply physics on the
player like jumping.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public float jumpSpeed;
// Start is called once before the first execution of Update after the
MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)){
rb.linearVelocity = Vector2.up *
jumpSpeed;
}
}
}

By Khan Azish Ahmed 19


Pipe Movement Script
Now Instead making our player move we will make the pipes move to the left.
Note here we added a new variable Time.deltaTime;
(Represents the time in seconds instead of per frame. It make sures that the actions are
smooth and consistent regardless of devices.)
using UnityEngine;

public class PipeMovement : MonoBehaviour


{
[SerializeField] float speed;
// Start is called once before the first execution of Update after the
MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = transform.position
+ Vector3.left *
speed * Time.deltaTime;
}
}

By Khan Azish Ahmed 20


Pipe Spawner

•We will make the the scene movement


by making the pipe as a prefabricated
object(Prefab).

•Prefab: In Unity, a prefab is a reusable


GameObject template that stores all the
components, properties, and child
objects of the original GameObject.
Prefabs allow you to create multiple
consistent instances of an object in your
game.

•For creating prefab drag and drop the


gameObject in assets folder of your
project.

By Khan Azish Ahmed 21


• Now to make the pipes spawn we will
add the new gameObject and put just
Pipe Spawner after the player where we need them to
spawn.
• Add a monobehaviour Script component
to it for the spawn behaviour.

By Khan Azish Ahmed 22


Pipe Spawner Script
Variable to add in the pipe spawner script.

// Reference to the pipe prefab that will be spawned


[SerializeField] GameObject pipe;
// The time interval (in seconds) between each pipe spawn
[SerializeField] float spawnRate = 2;
// A timer to keep track of time elapsed since the last
pipe was spawned
private float timer = 0;

By Khan Azish Ahmed 23


Pipe Spawner Script
Logic applied in update method to get the pipe spawned after a specific
timer value is valid to make them spawn at intervals.

void Update()
{
// If the timer is less than the defined spawn rate
if (timer < spawnRate)
{
// Increment the timer by the time taken for
the current frame
timer += Time.deltaTime;
}
else
{
// Spawn a new pipe at the spawner's position
and rotation
Instantiate(pipe, transform.position,
transform.rotation);
// Reset the timer to zero to start counting
for the next spawn
timer = 0;
}
}
By Khan Azish Ahmed 24
Pipe Spawner Script
We have issue that first pipe spawns after a very long interval so we will
add the instantiate in start method as well. But instead of making multiple
copies of the instantiate method we will do by making a different method
for it.

By Khan Azish Ahmed 25


Pipe Spawner Script
This will generate the pipes at different heights rather than generating
them at the same position Y axis.

By Khan Azish Ahmed 26


Extra Pipes Problem
Now here we have an issue in which there are pipes being spawned but
gameobjects are not deleting. This causes load on the RAM and resources
so we will try to resolve this issue.

By Khan Azish Ahmed 27


Pipe Move Script
We have set the limit zone for the destruction of pipes. And gave if
statement for the destruction if that limit is reached with debug message.

By Khan Azish Ahmed 28


Importing Assets
Assets store-> flappy birds -> import the below mentioned.
Later in unity: Window -> Package Manager -> My Assets

By Khan Azish Ahmed 29


Sprite Editor

A sprite sheet is a single image


file that contains multiple
images
• Sprite Editor, you can split
up the images into sprites that
can be used throughout your
project on any script or
component that accepts a
Sprite variable type.
-Basically these sprites will be
used to create our animations.

By Khan Azish Ahmed 30


Sprite Editor

• Adding the sprite


image.
Drag and Drop the sprite
to the sprite of the
player

By Khan Azish Ahmed 31


Sprite Editor

• Do the same for Pipe


prefab.
• Select it from the
assets prefab folder by
double click and add
for both up and down
gameObject.

By Khan Azish Ahmed 32


Adjustments in
Box Collider

We need to modify the Box


collider 2D too.

For this select the edit


collider from tools or from
the box collider2D
component in the inspector
window.

By Khan Azish Ahmed 33


Animations for the
player

1. Create Animations
folder
2. Create a new
Animation controller
and Animation Clip in
the folder
3. Add them to the Player
by drag & drop to
inspector windows.

By Khan Azish Ahmed 34


Animations for the
player

1. Create Animations
folder.
2. Create a new Animation
controller int the folder.
3. Add them to the Player
by drag & drop to
inspector windows.

By Khan Azish Ahmed 35


Animations for
the player

1. We will create the


Animation for the player.
2. Open the Animator &
Animation windows if
you closed them by
mistake follow this.

By Khan Azish Ahmed 36


State Machine
Animations for • Green: entry point
the player • Orange: default state
• Rectangles: states
• Arrows: transitions

By Khan Azish Ahmed 37


State Machine

More Complex • Green: entry point


• Orange: default state
Example • Rectangles: states
• Arrows: transitions

By Khan Azish Ahmed 38


• For Adding the Animation clips select the
player and in animation window select
Animations for the •
create and save the file.
DO NOT forget to press the record
player button as your sprites will not load in the
animation clip.
• To Add the sprites just search them and
clip at specific interval.

By Khan Azish Ahmed 39


• Now To make the clip joined with the
player go to the animator state
Animations for window.
the player • Select the Flying state and in motion
add the animation clip you made and
saved.

By Khan Azish Ahmed 40


Logics and UI
• Now to create the Score UI for our game we will use TextMeshPro
UI.
(TextMesh Pro is a set of Unity tools for 2D and 3D text. TextMesh Pro
provides better control over text formatting and layout than to Unity UI
Text.)

By Khan Azish Ahmed 41


Logics and UI
• Canvas: all UI elements should be inside of a Canvas object
• The canvas is bigger than the actual scene, so you need to
zoom out a lot.

By Khan Azish Ahmed 42


Logics and UI
Scripts
• Now add a new gameObject “Logic Manager”.
• Add new script to it.

By Khan Azish Ahmed 43


Logics and UI
Scripts
• Now add this script to and drag and drop text UI to the empty
text field in the Interface.
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class LogicScript : MonoBehaviour
{
// Start is called once before the first execution of
Update after the MonoBehaviour is created
public int playerScore;
public TMP_Text scoreText;
[ContextMenu("Increase Score")] //To manually see the
score updates from inspector
public void addScore(int scoreToAdd)
{
playerScore += scoreToAdd; //add score 1 to player
scoreText.text = playerScore.ToString(); //change
the text to playerscore
}
}
By Khan Azish Ahmed 44
Logics and UI
• Now for the collision add score logic physics we will add new
gameObject in pipe middle.
• Attach a box collider to it and make it trigger only.
• Also add new script to it to make logic.

By Khan Azish Ahmed 45


Logics and UI
Scripts
• Add this to the PipeMiddle Script and also make the Logic
Manger tag change to “logic” and player layer to “player”
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PipeMiddle : MonoBehaviour
{
public LogicScript logic;
// Start is called once before the first execution of Update
after the MonoBehaviour is created
void Start()
{
logic =
GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicScrip
t>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.layer == 3){
logic.addScore(1);
} By Khan Azish Ahmed 46
}
Game Over UI
We will create a new Game Object and add text and button Ui to it.

By Khan Azish Ahmed 47


Game Over UI
• Add this to Logic Script and in the button Text UI in Onclick section
• Drag&drop the Logic Manager gameObject and set to Restart
Game.
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

By Khan Azish Ahmed 48


• Add these changes in the Logic Manager
Game Over UI Script.
• Drag and drop the Game Over Screen Object
we created in the Canvas for Game Over.

By Khan Azish Ahmed 49


• Now in the PlayerController Script make
Game Over UI these changes to trigger the collision for the
game Over Screen to appear and Object to
die.

By Khan Azish Ahmed 50


• Add these changes in the Logic Manager
Game Over UI Script.
• Drag and drop the Game Over Screen Object
we created in the Canvas for Game Over.

By Khan Azish Ahmed 51


Audio and Sound Effects
Add a new Audio Source Component in Logic Manager and add the sound effects
for score.
Add a script code for playing it whenever the score is added to the player.

By Khan Azish Ahmed 52

You might also like