Roll-a-Ball (Unity): различия между версиями

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску
Строка 2: Строка 2:
  
 
== Setting up the Game ==
 
== Setting up the Game ==
=== 1.1 Create a new Unity project ===
+
=== Create a new Unity project ===
 
# Login
 
# Login
 
# New Project - Universal Render Pipeline
 
# New Project - Universal Render Pipeline
 
 
=== 1.2 Create a new Scene ===
+
=== Create a new Scene ===
 
# Layout
 
# Layout
 
# All files in "Template" folder
 
# All files in "Template" folder
Строка 12: Строка 12:
 
# File - Save as - Minigame (Scenes folder)
 
# File - Save as - Minigame (Scenes folder)
 
 
=== 1.3 Create a primitive plane ===
+
=== Create a primitive plane ===
 
# Add GameObject - Plane
 
# Add GameObject - Plane
 
# Rename "Ground"
 
# Rename "Ground"
Строка 19: Строка 19:
 
# Grid settings
 
# Grid settings
 
 
=== 1.4 Scale the Ground plane ===
+
=== Scale the Ground plane ===
 
# Scale  x-2 y-1 z-2
 
# Scale  x-2 y-1 z-2
  
=== 1.5 Create a player GameObject ===
+
=== Create a player GameObject ===
 
# Add GameObject - Sphere
 
# Add GameObject - Sphere
 
# Reset transform
 
# Reset transform
Строка 29: Строка 29:
 
# Elevate Sphere y +0.5
 
# Elevate Sphere y +0.5
  
=== 1.6 Adjust the default lighting ===
+
=== Adjust the default lighting ===
 
# Game view
 
# Game view
 
# Directional light - set color to white
 
# Directional light - set color to white
  
=== 1.7 Add colors with Materials ===
+
=== Add colors with Materials ===
 
# Create materials folder
 
# Create materials folder
 
# Create background material - 130/130/130 - metallic - 0 / smoothness - 0.25
 
# Create background material - 130/130/130 - metallic - 0 / smoothness - 0.25
Строка 41: Строка 41:
  
 
== Moving the player ==
 
== Moving the player ==
=== 2.1 Add a Rigidbody to the player ===
+
=== Add a Rigidbody to the player ===
 
# Add physics component to player - Rigidbody
 
# Add physics component to player - Rigidbody
  
=== 2.2 Install the Input System package ===
+
=== Install the Input System package ===
 
# Window - Package manager - Input system - Install
 
# Window - Package manager - Input system - Install
 
# Enable new backend system - Yes
 
# Enable new backend system - Yes
 
# (Windows only) File - Build settings - Architecture - x86_64
 
# (Windows only) File - Build settings - Architecture - x86_64
  
=== 2.3 Add a Player Input component ===
+
=== Add a Player Input component ===
 
# Add Player Input component to player
 
# Add Player Input component to player
 
# Create Input actions - "Create actions" button
 
# Create Input actions - "Create actions" button
Строка 55: Строка 55:
 
# In Player Input - Actions field choose created Asset
 
# In Player Input - Actions field choose created Asset
  
=== 2.4 Create a new script ===
+
=== Create a new script ===
 
# Create "Scripts" folder
 
# Create "Scripts" folder
 
# Add "PlayerController" script to player
 
# Add "PlayerController" script to player
  
=== 2.5 Write the OnMove function declaration ===
+
=== Write the OnMove function declaration ===
 
# Remove Update function
 
# Remove Update function
 
# Namespaces
 
# Namespaces
Строка 65: Строка 65:
 
# Create OnMove function (InputValue movementValue)
 
# Create OnMove function (InputValue movementValue)
  
=== 2.6 Apply input data to the Player ===
+
=== Apply input data to the Player ===
 
Vector2 movementVector = movementValue.Get<Vector2>();
 
Vector2 movementVector = movementValue.Get<Vector2>();
  
Строка 75: Строка 75:
 
Add FixedUpdate() function
 
Add FixedUpdate() function
  
=== 2.7 Apply force to the Player ===
+
=== Apply force to the Player ===
 
private float movementX;
 
private float movementX;
 
private float movementY;
 
private float movementY;
Строка 87: Строка 87:
 
rb.AddForce(movement);
 
rb.AddForce(movement);
  
=== 2.8 Fix the Player movement speed ===
+
=== Fix the Player movement speed ===
 
public float speed = 0;
 
public float speed = 0;
 
rb.AddForce(movement * speed);
 
rb.AddForce(movement * speed);
Строка 93: Строка 93:
  
 
== Moving the camera ==
 
== Moving the camera ==
=== 3.1 Set the Camera position ===
+
=== Set the Camera position ===
 
# Set camera position x=0 y=10 z=-10
 
# Set camera position x=0 y=10 z=-10
 
# Rotate x=45 y=0 z=0
 
# Rotate x=45 y=0 z=0
 
# Camera child of Player but will rotate with it
 
# Camera child of Player but will rotate with it
  
=== 3.2 Write a CameraController script ===
+
=== Write a CameraController script ===
 
Create CameraController script for camera
 
Create CameraController script for camera
 
public GameObject player;
 
public GameObject player;
Строка 110: Строка 110:
 
transform.position = player.transform.position + offset;
 
transform.position = player.transform.position + offset;
  
=== 3.3 Reference the Player GameObject ===
+
=== Reference the Player GameObject ===
 
# Drag player into player slot of the camera script
 
# Drag player into player slot of the camera script
  
 
== Setting up the Play Area ==
 
== Setting up the Play Area ==
=== 4.1 Create a wall for the play field ===
+
=== Create a wall for the play field ===
 
# Create empty object as root for Walls
 
# Create empty object as root for Walls
 
# Create Cube as West/East/North/South walls
 
# Create Cube as West/East/North/South walls
Строка 121: Строка 121:
 
# New Material Walls - 79/79/79 - metallic 0 / smoothness - 0.25
 
# New Material Walls - 79/79/79 - metallic 0 / smoothness - 0.25
  
=== 4.2 Finish the play field walls ===
+
=== Finish the play field walls ===
 
# Duplicate object
 
# Duplicate object
 
# Rotate or rescale
 
# Rotate or rescale
  
 
== Creating collectibles ==
 
== Creating collectibles ==
=== 5.1 Create a collectible GameObject ===
+
=== Create a collectible GameObject ===
 
# Add cube
 
# Add cube
 
# Move up 0.5
 
# Move up 0.5
Строка 133: Строка 133:
 
# Material Pickup color 255 200 0
 
# Material Pickup color 255 200 0
  
=== 5.2 Rotate the PickUp GameObject ===
+
=== Rotate the PickUp GameObject ===
 
Add Rotator script to pickup
 
Add Rotator script to pickup
 
Remove Start function
 
Remove Start function
Строка 139: Строка 139:
 
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
 
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
  
=== 5.3 Make PickUp a Prefab ===
+
=== Make PickUp a Prefab ===
 
# Create Prefabs folder
 
# Create Prefabs folder
 
# Open Prefab edit mode
 
# Open Prefab edit mode
  
=== 5.4 Add more collectibles ===
+
=== Add more collectibles ===
 
# New Empty Object - PickupParent
 
# New Empty Object - PickupParent
 
# Reset transform
 
# Reset transform
Строка 149: Строка 149:
  
 
== Detecting Collisions with Collectibles ==
 
== Detecting Collisions with Collectibles ==
=== 6.1 Disable PickUps with OnTriggerEnter ===
+
=== Disable PickUps with OnTriggerEnter ===
 
PlayerController create OnTriggerEnter(Collider other)
 
PlayerController create OnTriggerEnter(Collider other)
 
other.gameObject.SetActive(false);
 
other.gameObject.SetActive(false);
  
=== 6.2 Add a tag to the PickUp Prefab ===
+
=== Add a tag to the PickUp Prefab ===
 
# Add Pickup tag to PickUp Prefab
 
# Add Pickup tag to PickUp Prefab
 
# Create & Apply
 
# Create & Apply
  
=== 6.3 Write a conditional statement ===
+
=== Write a conditional statement ===
 
OnTriggerEnter
 
OnTriggerEnter
 
if (other.gameObject.CompareTag("PickUp"))
 
if (other.gameObject.CompareTag("PickUp"))
  
=== 6.4 Set the PickUp Colliders as triggers ===
+
=== Set the PickUp Colliders as triggers ===
 
Set Pickup Collider prefab as IsTrigger
 
Set Pickup Collider prefab as IsTrigger
  
=== 6.5 Add a Rigidbody to the PickUp Prefab ===
+
=== Add a Rigidbody to the PickUp Prefab ===
 
# Add to improve performance
 
# Add to improve performance
 
# Disable Use Gravity
 
# Disable Use Gravity
Строка 170: Строка 170:
  
 
== Displaying Score and Text ==
 
== Displaying Score and Text ==
=== 7.1 Store the value of collected PickUps ===
+
=== Store the value of collected PickUps ===
 
PlayerController
 
PlayerController
 
private int count;
 
private int count;
Строка 180: Строка 180:
 
count++;
 
count++;
  
=== 7.2 Create a UI text element ===
+
=== Create a UI text element ===
 
# Add UI => Text - TextMeshPro
 
# Add UI => Text - TextMeshPro
 
# Import TMP Essentials
 
# Import TMP Essentials
Строка 189: Строка 189:
 
# PosX = 10 PosY = -10
 
# PosX = 10 PosY = -10
  
=== 7.3 Display the count value ===
+
=== Display the count value ===
 
PlayerController
 
PlayerController
 
Add TMPro namespace
 
Add TMPro namespace
Строка 203: Строка 203:
 
EventSystem push "Replace with InputSystemUIInputModule"
 
EventSystem push "Replace with InputSystemUIInputModule"
  
=== 7.4 Create a game end message ===
+
=== Create a game end message ===
 
new TextMeshPro
 
new TextMeshPro
 
Text - Black size 32
 
Text - Black size 32
Строка 219: Строка 219:
  
 
== Build the Game ==
 
== Build the Game ==
=== 8.1 Create a build of your game ===
+
=== Create a build of your game ===
 
Save ALL
 
Save ALL
 
Build Settings
 
Build Settings

Версия 11:55, 8 августа 2022

Setting up the Game

Create a new Unity project

  1. Login
  2. New Project - Universal Render Pipeline

Create a new Scene

  1. Layout
  2. All files in "Template" folder
  3. File - New scene
  4. File - Save as - Minigame (Scenes folder)

Create a primitive plane

  1. Add GameObject - Plane
  2. Rename "Ground"
  3. Reset transform
  4. F to focus
  5. Grid settings

Scale the Ground plane

  1. Scale x-2 y-1 z-2

Create a player GameObject

  1. Add GameObject - Sphere
  2. Reset transform
  3. F to focus
  4. Unity unit - 1 meter
  5. Elevate Sphere y +0.5

Adjust the default lighting

  1. Game view
  2. Directional light - set color to white

Add colors with Materials

  1. Create materials folder
  2. Create background material - 130/130/130 - metallic - 0 / smoothness - 0.25
  3. Create player material - 0/220/255 - metallic - 0 / smoothness - 0.75
  4. Drag material to object
  5. Directional light rotation: x-50 y-50 z-0

Moving the player

Add a Rigidbody to the player

  1. Add physics component to player - Rigidbody

Install the Input System package

  1. Window - Package manager - Input system - Install
  2. Enable new backend system - Yes
  3. (Windows only) File - Build settings - Architecture - x86_64

Add a Player Input component

  1. Add Player Input component to player
  2. Create Input actions - "Create actions" button
  3. Create "Input" folder - save action "InputActions"
  4. In Player Input - Actions field choose created Asset

Create a new script

  1. Create "Scripts" folder
  2. Add "PlayerController" script to player

Write the OnMove function declaration

  1. Remove Update function
  2. Namespaces
  3. Add namespace "UnityEngine.InputSystem"
  4. Create OnMove function (InputValue movementValue)

Apply input data to the Player

Vector2 movementVector = movementValue.Get<Vector2>();

private Rigidbody rb;

In Start function rb = GetComponent<Rigidbody>();

Add FixedUpdate() function

Apply force to the Player

private float movementX; private float movementY;

In OnMove movementX = movementVector.x; movementY = movementVector.y;

In FixedUpdate Vector3 movement = new Vector3(movementX, 0.0f, movementY); rb.AddForce(movement);

Fix the Player movement speed

public float speed = 0; rb.AddForce(movement * speed); speed in Editor = 10

Moving the camera

Set the Camera position

  1. Set camera position x=0 y=10 z=-10
  2. Rotate x=45 y=0 z=0
  3. Camera child of Player but will rotate with it

Write a CameraController script

Create CameraController script for camera public GameObject player; private Vector3 offset;

In Start function calculate offset offset = transform.position - player.transform.position;

Update functions order undefined so we use LateUpdate In LateUpdate function transform.position = player.transform.position + offset;

Reference the Player GameObject

  1. Drag player into player slot of the camera script

Setting up the Play Area

Create a wall for the play field

  1. Create empty object as root for Walls
  2. Create Cube as West/East/North/South walls
  3. Scale x=0.5 y=2 z=20.5
  4. Position -10
  5. New Material Walls - 79/79/79 - metallic 0 / smoothness - 0.25

Finish the play field walls

  1. Duplicate object
  2. Rotate or rescale

Creating collectibles

Create a collectible GameObject

  1. Add cube
  2. Move up 0.5
  3. Scale 0.5
  4. Rotate 45 45 45
  5. Material Pickup color 255 200 0

Rotate the PickUp GameObject

Add Rotator script to pickup Remove Start function in Update function transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);

Make PickUp a Prefab

  1. Create Prefabs folder
  2. Open Prefab edit mode

Add more collectibles

  1. New Empty Object - PickupParent
  2. Reset transform
  3. Duplicate Pickup and move

Detecting Collisions with Collectibles

Disable PickUps with OnTriggerEnter

PlayerController create OnTriggerEnter(Collider other) other.gameObject.SetActive(false);

Add a tag to the PickUp Prefab

  1. Add Pickup tag to PickUp Prefab
  2. Create & Apply

Write a conditional statement

OnTriggerEnter if (other.gameObject.CompareTag("PickUp"))

Set the PickUp Colliders as triggers

Set Pickup Collider prefab as IsTrigger

Add a Rigidbody to the PickUp Prefab

  1. Add to improve performance
  2. Disable Use Gravity
  3. Check IsKinematic - no Physics

Displaying Score and Text

Store the value of collected PickUps

PlayerController private int count;

Start function count = 0;

OnTriggerEnter count++;

Create a UI text element

  1. Add UI => Text - TextMeshPro
  2. Import TMP Essentials
  3. All UI in Canvas
  4. Rename New Text CountText
  5. Add CountText as placeholder
  6. Anchor text left top with Alt + Shift
  7. PosX = 10 PosY = -10

Display the count value

PlayerController Add TMPro namespace public TextMeshProUGUI countText;

new function SetCountText countText.text = "Count: " + count.ToString();

Add SetCountText() to Start and OnTriggerEnter

Drop Text object to script

EventSystem push "Replace with InputSystemUIInputModule"

Create a game end message

new TextMeshPro Text - Black size 32 Set Text - You win! PosX = 0 posY = 130 Align center

Add reference to text as GameObject Disable in Start function - SetActive(false);

In SetCountText function if (count >= 12) { winTextObject.SetActive(true); }

Drop WinText in script

Build the Game

Create a build of your game

Save ALL Build Settings For web build 2020 disable compression Choose platform Add open scenes Scenes list Player settings Full screen or windowed