Roll-a-Ball (Unity)

Материал из Информационная безопасностя
Версия от 11:50, 8 августа 2022; Безуглов Сергей (обсуждение | вклад) (Новая страница: «{{TOCRight}} === 1.1 Create a new Unity project === # Login # New Project - Universal Render Pipeline === 1.2 Create a new Scene === # Layout # All files in "T...»)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к навигации Перейти к поиску

1.1 Create a new Unity project

  1. Login
  2. New Project - Universal Render Pipeline

1.2 Create a new Scene

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

1.3 Create a primitive plane

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

1.4 Scale the Ground plane

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

1.5 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

1.6 Adjust the default lighting

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

1.7 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

2.1 Add a Rigidbody to the player

  1. Add physics component to player - Rigidbody

2.2 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

2.3 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

2.4 Create a new script

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

2.5 Write the OnMove function declaration

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

2.6 Apply input data to the Player

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

private Rigidbody rb;

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

Add FixedUpdate() function

2.7 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);

2.8 Fix the Player movement speed

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

3.1 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

3.2 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;

3.3 Reference the Player GameObject

  1. Drag player into player slot of the camera script

4.1 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

4.2 Finish the play field walls

  1. Duplicate object
  2. Rotate or rescale

5.1 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

5.2 Rotate the PickUp GameObject

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

5.3 Make PickUp a Prefab

  1. Create Prefabs folder
  2. Open Prefab edit mode

5.4 Add more collectibles

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

6.1 Disable PickUps with OnTriggerEnter

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

6.2 Add a tag to the PickUp Prefab

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

6.3 Write a conditional statement

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

6.4 Set the PickUp Colliders as triggers

Set Pickup Collider prefab as IsTrigger

6.5 Add a Rigidbody to the PickUp Prefab

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

7.1 Store the value of collected PickUps

PlayerController private int count;

Start function count = 0;

OnTriggerEnter count++;

7.2 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

7.3 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"

7.4 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

8.1 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