Make a coin

The coin object

  1. In the Hierarchy, select + and choose 3D Object ▸ Cylinder. Rename it Coin.
  2. Set its Rotation to 90, 0, 0 and its Scale to 1, 0.1, 1.
  3. Drag Gold.mat onto Material 0 in its MeshRenderer.
  4. 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 selected in the editor: its Rotation and Scale, Gold.mat on Material 0, and isTrigger selected

The Coin component

  1. Select Scripts ▸ New C++ Component..., enter Coin as the Class name and select Create.
  2. Replace the contents of the two new files:
Source/Coin.h
#pragma once

#include "Shalltear.h"

// coin player can collect. It spins so it catches the eye.. the player
// is what notices the touch -> collects it (PlayerController)
class Coin : public Behavior {
public:
	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;
};
Source/Coin.cpp
#include "Coin.h"

SLT_REGISTER_TYPE(Coin)

void Coin::OnUpdate() {
	SceneObject *object = GetOwner().Get();
	if (!object) {
		return;
	}
	// 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
	// (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());
}
  1. Save both files. The editor compiles them, and Coin appears in Add Component.

  2. Select the coin and add the Coin component.

    The Coin component on the coin, with its value and degreesPerSecond fields

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.

  1. Drag the Coin row from the Hierarchy onto the Asset Browser. The editor saves it as Coin.prefab, and the coin in the scene becomes a copy of it.

    Coin.prefab in the Asset Browser, and the coin's row in the Hierarchy with the prefab icon
  2. Drag Coin.prefab from the Asset Browser into the Scene View four times, so the level has five coins.

    Coin.prefab being dragged from the Asset Browser into the Scene View
  3. Set each coin's Position:

CoinPosition
1-5, 0.75, 5
25, 0.75, 5
30, 0.75, 6
4-5, 0.75, -5
55, 0.75, -5
The five coins in the Scene View, with the first coin's Position in the Inspector

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.

The five coins spinning in the Runtime View
Coin.prefab open for editing: the Hierarchy shows the prefab alone, with Scenes above it to go back

Save the scene. Next, move the player.