Roll-a-Ball (Unity)

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску

Setting up the Game

Create a new Unity project

  1. Login
  2. New Project - 3D (URP) - 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
  3. Directional light rotation: x-50 y-50 z-0

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

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

In PlayerController

1 Vector2 movementVector = movementValue.Get<Vector2>();
2 
3 private Rigidbody rb;

In Start function

1 rb = GetComponent<Rigidbody>();

Add FixedUpdate() function

Apply force to the Player

1 private float movementX;
2 private float movementY;

In OnMove

1 movementX = movementVector.x;
2 movementY = movementVector.y;

In FixedUpdate

1 Vector3 movement = new Vector3(movementX, 0.0f, movementY);
2 rb.AddForce(movement);

Fix the Player movement speed

1 public float speed = 10;
2 rb.AddForce(movement * speed);
  • speed in Editor

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

1 public GameObject player;
2 private Vector3 offset;

In Start function calculate offset

1 offset = transform.position - player.transform.position;

Update functions order undefined so we use LateUpdate In LateUpdate function

1 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 - metallic 0 / smoothness - 0.25

Rotate the PickUp GameObject

Add Rotator script to pickup Remove Start function in Update function

1 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

1 PlayerController create OnTriggerEnter(Collider other)
2 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

1 OnTriggerEnter
2 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

1 private int count;

Start function

1 count = 0;

OnTriggerEnter

1 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

1 using TMPro;
2 public TextMeshProUGUI countText;

new function SetCountText

1 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

  1. Text - Black size 32
  2. Set Text - You win!
  3. PosX = 0 posY = 130
  4. Align center

Add reference to text as GameObject

Disable in Start function - SetActive(false);

In SetCountText function

1 if (count >= 12)
2 {
3 	winText.gameObject.SetActive(true);
4 }

Drop WinText in script

Build the Game

Create a build of your game

  1. Save ALL
  2. Build Settings
  3. For web build 2020 disable compression
  4. Choose platform
  5. Add open scenes
  6. Scenes list
  7. Player settings
  8. Full screen or windowed