Roll-a-Ball (Unity): различия между версиями
(Новая страница: «{{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: | ||
{{TOCRight}} | {{TOCRight}} | ||
+ | == Setting up the Game == | ||
=== 1.1 Create a new Unity project === | === 1.1 Create a new Unity project === | ||
# Login | # Login | ||
Строка 39: | Строка 40: | ||
# Directional light rotation: x-50 y-50 z-0 | # Directional light rotation: x-50 y-50 z-0 | ||
+ | == Moving the player == | ||
=== 2.1 Add a Rigidbody to the player === | === 2.1 Add a Rigidbody to the player === | ||
# Add physics component to player - Rigidbody | # Add physics component to player - Rigidbody | ||
Строка 90: | Строка 92: | ||
speed in Editor = 10 | speed in Editor = 10 | ||
+ | == Moving the camera == | ||
=== 3.1 Set the Camera position === | === 3.1 Set the Camera position === | ||
# Set camera position x=0 y=10 z=-10 | # Set camera position x=0 y=10 z=-10 | ||
Строка 109: | Строка 112: | ||
=== 3.3 Reference the Player GameObject === | === 3.3 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 == | ||
=== 4.1 Create a wall for the play field === | === 4.1 Create a wall for the play field === | ||
# Create empty object as root for Walls | # Create empty object as root for Walls | ||
Строка 121: | Строка 125: | ||
# Rotate or rescale | # Rotate or rescale | ||
+ | == Creating collectibles == | ||
=== 5.1 Create a collectible GameObject === | === 5.1 Create a collectible GameObject === | ||
# Add cube | # Add cube | ||
Строка 143: | Строка 148: | ||
# Duplicate Pickup and move | # Duplicate Pickup and move | ||
+ | == Detecting Collisions with Collectibles == | ||
=== 6.1 Disable PickUps with OnTriggerEnter === | === 6.1 Disable PickUps with OnTriggerEnter === | ||
PlayerController create OnTriggerEnter(Collider other) | PlayerController create OnTriggerEnter(Collider other) | ||
Строка 163: | Строка 169: | ||
# Check IsKinematic - no Physics | # Check IsKinematic - no Physics | ||
+ | == Displaying Score and Text == | ||
=== 7.1 Store the value of collected PickUps === | === 7.1 Store the value of collected PickUps === | ||
PlayerController | PlayerController | ||
Строка 211: | Строка 218: | ||
Drop WinText in script | Drop WinText in script | ||
+ | == Build the Game == | ||
=== 8.1 Create a build of your game === | === 8.1 Create a build of your game === | ||
Save ALL | Save ALL |
Версия 11:54, 8 августа 2022
Setting up the Game
1.1 Create a new Unity project
- Login
- New Project - Universal Render Pipeline
1.2 Create a new Scene
- Layout
- All files in "Template" folder
- File - New scene
- File - Save as - Minigame (Scenes folder)
1.3 Create a primitive plane
- Add GameObject - Plane
- Rename "Ground"
- Reset transform
- F to focus
- Grid settings
1.4 Scale the Ground plane
- Scale x-2 y-1 z-2
1.5 Create a player GameObject
- Add GameObject - Sphere
- Reset transform
- F to focus
- Unity unit - 1 meter
- Elevate Sphere y +0.5
1.6 Adjust the default lighting
- Game view
- Directional light - set color to white
1.7 Add colors with Materials
- Create materials folder
- Create background material - 130/130/130 - metallic - 0 / smoothness - 0.25
- Create player material - 0/220/255 - metallic - 0 / smoothness - 0.75
- Drag material to object
- Directional light rotation: x-50 y-50 z-0
Moving the player
2.1 Add a Rigidbody to the player
- Add physics component to player - Rigidbody
2.2 Install the Input System package
- Window - Package manager - Input system - Install
- Enable new backend system - Yes
- (Windows only) File - Build settings - Architecture - x86_64
2.3 Add a Player Input component
- Add Player Input component to player
- Create Input actions - "Create actions" button
- Create "Input" folder - save action "InputActions"
- In Player Input - Actions field choose created Asset
2.4 Create a new script
- Create "Scripts" folder
- Add "PlayerController" script to player
2.5 Write the OnMove function declaration
- Remove Update function
- Namespaces
- Add namespace "UnityEngine.InputSystem"
- 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
Moving the camera
3.1 Set the Camera position
- Set camera position x=0 y=10 z=-10
- Rotate x=45 y=0 z=0
- 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
- Drag player into player slot of the camera script
Setting up the Play Area
4.1 Create a wall for the play field
- Create empty object as root for Walls
- Create Cube as West/East/North/South walls
- Scale x=0.5 y=2 z=20.5
- Position -10
- New Material Walls - 79/79/79 - metallic 0 / smoothness - 0.25
4.2 Finish the play field walls
- Duplicate object
- Rotate or rescale
Creating collectibles
5.1 Create a collectible GameObject
- Add cube
- Move up 0.5
- Scale 0.5
- Rotate 45 45 45
- 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
- Create Prefabs folder
- Open Prefab edit mode
5.4 Add more collectibles
- New Empty Object - PickupParent
- Reset transform
- Duplicate Pickup and move
Detecting Collisions with Collectibles
6.1 Disable PickUps with OnTriggerEnter
PlayerController create OnTriggerEnter(Collider other) other.gameObject.SetActive(false);
6.2 Add a tag to the PickUp Prefab
- Add Pickup tag to PickUp Prefab
- 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
- Add to improve performance
- Disable Use Gravity
- Check IsKinematic - no Physics
Displaying Score and Text
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
- Add UI => Text - TextMeshPro
- Import TMP Essentials
- All UI in Canvas
- Rename New Text CountText
- Add CountText as placeholder
- Anchor text left top with Alt + Shift
- 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
Build the Game
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