Make a coin
The coin object
- In the Hierarchy, select + and choose 3D Object ▸ Cylinder. Rename it
Coin. - Set its Rotation to
90, 0, 0and its Scale to1, 0.1, 1. The cylinder is now a flat disc standing on its edge. - Drag
Gold.matonto Material 0 in its MeshRenderer. - Add a BoxCollider and select isTrigger. A trigger collider notices what passes through it instead of blocking it, so the player can run into the coin and pick it up.
The Coin component
- Select Scripts ▸ New C++ Component..., enter
Coinas the Class name and select Create. - Replace the contents of the two new files:
#pragma once
#include "Shalltear.h"
// a coin the player can collect. It spins so it catches the eye; the player
// is what notices the touch and collects it (PlayerController)
class Coin : public Behavior {
public:
Coin() : Behavior(BehaviorTick::Update) {}
SLT_REFLECT_COMPONENT(Coin, Behavior)
SLT_REFLECT_FIELD(value)
SLT_REFLECT_FIELD(degreesPerSecond)
SLT_END_REFLECT()
// how many coins this one counts as
i32 value = 1;
f32 degreesPerSecond = 90.0f;
protected:
void OnUpdate() override;
};#include "Coin.h"
SLT_REGISTER_TYPE(Coin)
void Coin::OnUpdate() {
SceneObject* object = GetOwner().Get();
if (!object) {
return;
}
// the step goes on the LEFT, which turns the coin about the world's up
// axis. The coin is tipped on its edge, so its own up axis points sideways
// and a step on the right would roll it instead
const Quat step = Quat::AngleAxis(degreesPerSecond * Time::GetDeltaTime(), Vec3::Up());
object->SetLocalRotation((step * object->GetLocalRotation()).Normalized());
}- Save both files. The editor compiles them, and Coin appears in Add Component.
- Select the coin and add the Coin component.
A prefab, placed five times
A prefab is an object saved as an asset, so you can place copies of it that all follow the saved original.
- Drag the
Coinrow from the Hierarchy onto the Asset Browser. The editor saves it asCoin.prefab, and the coin in the scene becomes a copy of it. - Drag
Coin.prefabfrom the Asset Browser into the Scene View four times, so the level has five coins. - Set each coin's Position:
| Coin | Position |
|---|---|
| 1 | -5, 0.75, 5 |
| 2 | 5, 0.75, 5 |
| 3 | 0, 0.75, 6 |
| 4 | -5, 0.75, -5 |
| 5 | 5, 0.75, -5 |
Press Ctrl+P to play. The coins spin; press Ctrl+P again to stop. To change every coin at once later, double-click Coin.prefab to edit the prefab itself.
Save the scene. Next, move the player.