In this tutorial we’ll add random battles to our game.
For now, we’ll only add them to the Forest scene (Assets/Scenes/), so open it.
We’ll do two things:
- set the battle scene variable
- add a random battle area
Let’s get to it.
Battle Scene Variable #
Create a new empty game object using the scene hierarchy’s context menu: Create Empty
Change the newly created game object’s name to Set Battle Scene.
Variable Changer #
Add a Variable Changer component to the game object using the component menu.
Variable changers are used to change, well, variables – without having to go through a schematic. They can e.g. be started automatically when loading the scene (which we’ll do), when entering a trigger or started by interacting with them.
Variable Change Settings #
We’ll set the battleScene string variable to the scene name we want our battles to take place in.
Click on Add Variable.
- Change Type
We’ll directly change a variable instead of using a template.
- Variable Key
Set to: battleScene
- String Type
Select Value > Value.
I.e. the variable key is defined directly as a value, this is the standard way for variable keys, which you’ll use in 99.9% of the use cases.
- Variable Origin
- String Value
Set to: Battle Forest
You might notice that the string value fields automatically have all scene names available for selection (at least of the scenes added to the Unity build settings).
Start Settings #
These settings define how the variable change is started – we’ll use the Auto setting’s Start start type (i.e. the machine will start when the level was loaded or the game object was instantiated).
Components like the Variable Changer have quick-setup buttons available, the Autostart button will handle the setup for us.
Click on Autostart.
That’s it for the variable changer – now, each time we enter this scene, the battle scene used by the battle start schematic will automatically be set to the Battle Forest scene.
Adding Random Battles #
Next, we’ll set up the Random Battle Area. This component will start battles while the player moves within it’s trigger.
We can use the scene hierarchy’s context menu to create a ready-to-use setup for us: ORK Framework > Battle > Random Battle Area (2D)
This’ll create a new game object with a Random Battle Area component and a Box Collider 2D used as trigger.
Let’s adjust it to our needs.
We’ll position it at the center of the scene.
Set to X=0, Y=0, Z=0.
Random Battle Area #
Now, let’s set up how often a random battle happens and which enemies can appear.
Random Battle Settings #
The Random Chance Settings in combination with the distance settings determine how often a battle happens. We’ll keep the default settings for battle chance and check interval.
- Battle Chance
Set to 10.
There’s a 10% chance to start a battle when checked.
Seems pretty high on it’s own.
- Check Interval
Set to 0.5.
The time between performing 2 checks.
I.e. there are 2 checks per second possible.
We can control how often a battle starts via the distance settings.
- Minimum Move Distance
Set to 0.1.
The player needs to move at least 0.1 world units between checks.
- Minimum Distance
Set to 5.
The next battle can start after the player has moved at least 5 world units.
- Maximum Distance
Set to 15.
A battle will start without check if the player moved for 15 (or more) world units since the last battle.
Combatant Settings #
We’ll add multiple combatants/groups that will be used for random battles. Which one is used is determined by a chance check – each combatant/group we add defines the chance it has to occur.
The total of all chances should be 100 to have a full range of possible enemies. However, you can also define less or more – less would mean that if no enemy was found, no battle occurs. More, well, all exceeding the maximum chance (100 for all chance checks, can be changed in Game > Game Settings) would never occur. You can see the total chance of all added combatants/groups at the top of the component’s settings.
Let’s get to it. We’ll begin with editing the already added Combatant 0.
Set to 50.
- Use Group
Disable this setting.
Select Blue Mushroom.
Copy the Combatant 0 setup and change the following settings.
Set to 30.
Select Red Mushroom.
Copy the Combatant 1 setup and change the following settings.
Set to 20.
- Use Group
Enable this setting.
- Combatant Group
Select Mushroom x2.
That sums up to 100 percent – 50% chance to fight a blue mushroom, 30% chance to fight a red mushroom, 20% chance to fight 2 random mushrooms.
Box Collider 2D #
And finally, we’ll adjust the size of the box collider to wrap around the whole scene, basically the same as the camera border.
Set to X=40, Y=30.
That’s it for the random battle setup.
Save the changes you’ve made in the scene.
It’s finally time to fight! Hit play right in the Forest scene (since we earlier set up our game starter), or open the Town scene and go to the forest from there.
Everything seems to be working, we zoom into the battle and can attack the enemy (and get attacked or poisoned back).
Since our player doesn’t have any abilities (beside the base attack), the Ability battle menu option currently has nothing to display – also, there are no items in the player’s inventory, so the Item battle menu option is grayed out.
However, the actions appear to be happening at the same time. This is due to them not yet being animated, so they’re over in an instant.
So, next, we’ll start animating our actions.