Creating a Pressure Pad in Unity
New trigger for Puzzles, Actions or win the level
Objective: Create a script to detect when the box is on the pressure pad and change the color of the box to green.
In the scene there is a new prefab called Pressure_Pad is a simple cube on the floor with a collider attached. The first step is to create a new script, attach it to the pressure pad. And this is the logic to detect when the box is on the pad.
In the PressurePad script we have to use the function OnTriggerStay, check for the MovingBox tag. Then once we know the object colliding is the moving box we have to create a variable to calculate the distance between the box and the pressure pad.
Next we check if the distance is less than 0.5f (or a value that works for your game) within that condition you can trigger any action for your game like open a door or finish the level. In this case I just changed the color of the box to green and set the rigidbody of the box as kinematic to stop the movement.
And that’s it!!! We have a functional pressure pad to trigger actions on our game.