You can use Environmental damage dealers to create traps that damage the player when he steps into them.
Environmental damage dealers are Damage Dealer components that have Always On enabled. They don’t need to be activated by an action (i.e. they are active all the time), but still require a combatant that knows the used ability.
To get this going, we’ll create:
- a battle event to animate the trap
- a new ability for the trap
- a new combatant for environmental damage
- the trap
So let’s get to it!
A new battle event
We’ll create a small battle event that spawns a prefab (particle effect) when damaging a combatant. Open the ORK Framework editor and navigate to Events. Click on New Battle Event and change the following settings.
Event Settings
The trap will spawn a prefab on the target, so we’ll need to add it first.
- Destroy Prefabs
Disable this setting.
Click on Add Prefab.
- Prefab
Select Sparks2 or another prefab that you want to use.
Sparks2 will play quick spark particle effect.
Spawn Prefab
Add > Game Object > Prefab > Spawn Prefab
We’ll spawn the prefab on the target.
- Prefab
Select Prefab 0. - Object (Target Object)
Select Actor. - Actor
Select Target. - Local Space
Enable this setting. - Offset
Set to X=0, Y=1, Z=0.
Calculate
Add > Battle Steps > Calculate
This will calculate the damage of the ability.
Emit Particles
Add > Game Object > Component > Emit Particles
We’ll stop the particle effect shortly after spawning the prefab to create a nice spark effect.
- Emit
Disable this setting.
This will stop emitting particles. - Emit After (s)
Set to 0.1. - Object (Target Object)
Select Prefab. - Prefab
Select Prefab 0. - Spawned Prefab ID
Set to -1.
This will use all spawned instances of the prefab.
Destroy Prefab
Add > Game Object > Prefab > Destroy Prefab
We’ll destroy the prefab after some time. We stop emitting particles before destroying the prefab to prevent the particles from suddenly being destroyed.
- Prefab
Select Prefab 0. - Spawned Prefab ID
Set to -1. - Destroy After Time
Enable this setting. - Time (s)
Set to 2. - Wait
Disable this setting.
And that’s it for the event – click on Save Event and save it as trap.asset in Assets/Events/Battle/.
The trap ability
The trap will do damage using an ability, so navigate to Status > Abilities, add a new ability and change the following settings.
- Name
Set to Trap. - Useable In
Select Both.
Target Settings
- Target Type
Select Enemy.
If you want the trap to damage all combatants, you can select All. - Target Range
Select Single. - Is Dead
Select No.
Damage Dealer Settings
Environmental damage dealers are animated in the Damage Dealer Settings of the ability instead of the Battle Animation settings.
- Animate
Enable this setting.
Change the settings of the already added battle event.
- Battle Event
Select the trap event file we just created. - Chance
Set to 100. - Perform In
Enable Field and all battle system types.
The trap should do damage all the time.
Target Changes
The trap should do 50 damage to the HP and add a poison status effect. Click on Add Value Change to add the status value change for the damage to the target.
- Status Value
Select HP. - Operator
Select Sub. - Value Origin
Select Value. - Value
Set to 50.
Click on Add Status Effect to add a status effect change.
- Status Effect
Select Poison. - Change
Select Add.
And that’s it for the ability.
The enviromental combatant
In order to use the ability, the trap needs a combatant that can use the ability (i.e. he must know it). While you can use an already existing combatant, it’s best to create a new combatant only for traps and other environmental damage dealer uses. Navigate to Combatants > Combatants, add a new combatant and change the following settings.
- Name
Set to Environmental Damage. - Prefab
Select no prefab.
This combatant will never be spawned in the game, only used by damage dealers for calculations.
Status Settings
Currently we only use this combatant for a trap that doesn’t use the status values of the combatant, so we can ignore these settings. When you later add more environmental damage dealers and do damage based on stats, you can come back here and set it up the way you need it. Using Status Development, you can even create different levels of your traps.
Ability Development
The ability used by the environmental damage dealer must be known to the combatant and mustn’t be an attack or counter attack. Click on Add Ability to teach the ability we just created to the combatant.
- Learn at Level
Set to 1. - Ability
Select Trap.
And that’s it – click on Save Settings to save the changes.
Creating the trap
Open a scene (or create a new scene) – now we’ll be creating the trap’s game object and make it into a prefab for later use. Create a new game object using the Unity® menu: Game Object > Create Empty
Name the game object Trap and add the following components.
Box Collider
Add a Box Collider component and change the following settings.
- Is Trigger
Enable this setting. - Size
Set the size to an appropriate setting.
I’m using X=2, Y=2, Z=2 – since you can change this in the individual instances of the prefab, it’s not that important now.
Rigidbody
Add a Rigidbody component and change the following settings.
- Use Gravity
Disable this setting. - Is Kinematic
Enable this setting. - Freeze Position (Constraints)
Enable all axes. - Freeze Rotation (Constraints)
Enable all axes.
Damage Dealer
Add a Damage Dealer component and change the following settings.
- Type
Select Trigger Enter. - Always On
Enable this setting.
This will make the damage dealer an environmental damage dealer. - Ability
Select Trap. - Set Combatant
Enable this setting.
We will select a combatant here – otherwise, the damage dealer will search for a Combatant Component in case it’s part of a combatant (e.g. for contact damage). - Faction
Select Enemies. - Combatant
Select Environmental Damage. - One Time Damage
Disable this setting. - One Target
Disable this setting.
And that’s it – make the trap a prefab by dragging it from the scene hierarchy into the project view.
Now you can use the trap wherever you need it in your game by adding the prefab to the scene – you can also use it in the event system to spawn new instances of the trap.
Tip: Trap not working?
If your trap isn’t working, make sure your player combatant has a Damage Zone set up on it’s prefab. A damage dealer can only harm game objects with a damage zone.
Also, make sure the combatant you’re using for the trap isn’t dead, i.e. set the HP/health to a value that wont cause death in the combatant’s status settings.