Adding Traps
Published 3 months ago
Add traps linked to a button on the ground
When you talk about dungeons, then you're talking about traps!
You know.. those kind of traps that when you enter a room, you suddenly are dead. Let's start.
To begin with, we need a button on the floor, which our player can interact with. If you have a mesh you want to use for the button, now it's the time to drag and drop it in the scene.
If you don't have a mesh, don't worry create a new 3D Cube and scale it accordingly. Select the object and add a Sphere Collider to it. Set it to trigger and adjust the radius of the collider.
Create now a new script called Trap and a new script called TrapTrigger. The TrapTrigger will activate the Traps when we walk over the mesh. Let's start with the TrapTrigger script.
using UnityEngine; public class TrapTrigger : MonoBehaviour { // This is the array of traps. We can trigger more than one trap with a single trigger. public Trap[] Traps; // This is the distance the mesh will sink into the ground. public float Distance = 0.1f; // This is the speed the mesh will sink into the ground. public float Speed = 10; // This flag identifies if the button is triggered. private bool IsTriggered = false; // This flag identifies if the trap is active. private bool IsActive = true; void Update() { if (IsTriggered) { // When the trap is triggered, deactivate it (fires only once) IsActive = false; // Calculate the end position of the mesh button. var endPosition = new Vector3(transform.position.x, transform.position.y - Distance, transform.position.z); // Move the button to the end position, using a linear interpolation. // Use Speed * Time.deltaTime to get a linear movement that is the same with all fps. transform.position = Vector3.Lerp(transform.position, endPosition, Speed * Time.deltaTime); } } // On trigger enter is called every time a gameobject with a collider enters the Sphere Collider. void OnTriggerEnter(Collider other) { if (IsActive) { // If the trap IsActive, then we trigger it. IsTriggered = true; foreach (var trap in Traps) { // we go through all the traps, and we trigger the single trap. trap.TriggerTrap(); } } } }
This script needs to be attached to the button gameobject, bottom of the SphereCollider

Now that we have our trap trigger, we need the actual trap. If you want, you can have a mesh or you can just have a position.
Create a child object and call it LaunchTransform. Move the transform to the point where the arrow will come out for you, pointing the Z axis as a direction.
At this point add the Trap script to the main arrow trap object and fill in the script
using UnityEngine; public class Trap : MonoBehaviour { public Transform LaunchTransform; public GameObject ArrowGameObject; public void TriggerTrap() { // launch an arrow. Instantiate(ArrowGameObject, LaunchTransform.position, LaunchTransform.rotation); } }
Let's go back to our TrapTrigger and add this trap to its list
Drag and drop the Trap into the array and Unity will add it.
Now the only thing we need to add, is the actual Arrow. Let's get an arrow and drag it into the scene.
Make sure that the local Z of the arrow is pointing in the same direction of the... arrow. Drag the element from the Hierarchy to the folder explorer to create a prefab. Now that you have a prefab, you can drag this prefab into the Trap. Let's do it
At this point, if you try out the game and go over the platform, you will see the arrow is spawned.. but it's not moving! You can add a script to the arrow prefab to make it move. Here I have one for you
using UnityEngine; public class Projectile : MonoBehaviour { // The speed of the game object. public float Speed = 10f; // The time after the projectile will be destroyed. public float LifeTimeSeconds = 10; private float timer = 0f; void Update () { timer += Time.deltaTime; if (timer < LifeTimeSeconds) { // If it's not yet time to destroy the projectile, then move it forward. transform.Translate(Vector3.forward*Time.deltaTime*Speed); } else { // If time's up, then destroy this gameobject. // This is important or else you will end up with lots of objects in the Hierarchy // that are not useful. Destroy(this.gameObject); } } }
This ends this tutorial! You can see my character trying to avoid one arrow here! It didn't work out very well for him

Pietro Carta
Developer - Programmer