Pushing Objects in Unity to Complete Puzzles

Pushing Rigidbodies and activating pressure triggers.

James Lafritz
Dev Genius

--

The next thing I want to implement is adding the ability to push boxes around in my game to activate a trigger.

Pushing a Rigidbody

Lucky for me the Unity documentation has this as an example.

In the example they are pushing things using Rigidbody, making sure that they are not pushing things below it and they are moving things on the X and Z direction. I only need to worry about the X axis. I am going to break this down into it’s steps in my Behavior. I am also going to make a seperate method that can handle pushing of Rigidbodies.

I have a Moveable box in my scene nothing special about this create other then it has Rigidbody Component with the position frozen on the Z axis (So it does not move off the platform and the rotation on the y axis frozen (To keep it from spinning on it’s Y axis), along with a Box Collider. I then have a child for the visual. The child object just makes it easy for me to be able to swap it out for a different visual.

Now that I have the moveable box set up I can push it with my player.

If there is no rigidbody or the rigidbody is kinematic I exit the method.

I then make sure that I am not above the object that I want to push.

I calculate the move direction based on the hit move direction on the x axis.

I then set the rigidbody’s velocity to the move direction multiplied by the push power.

Now this in action. Notice the different effects that the rigidbody constraints have. If I had a bigger box to push I might freeze the rotation on the X Axis

I am also not taking the mass of the object into account when pushing it the only way that I can control that is by the amount of drag there is.

Since I am using hit.rigidbody a lot I decided to cache it.

I created a variable to hold the push power calculation. I check to see if the rigidbody mass is greater then 0 if it is I use that in my calculation if it is not then i use 0.1 in my calculation. I take that and divide by the push power amount. I then use this new variable in my calculations. I also added the range attribute to the player’s push power to keep it from going below 0.1

Pushing the box with different mass.

Looks like I got my math backwards. I need to divide the push power by the mass.

Now I am taking into account the mass of the object when pushing it. I can also make my character stronger.

Activating the Pressure Pad

There are several different ways to be able to activate a pressure pad. Some games have the Player activate it and it is treated like a switch, Some Games allow the Player to activate it when the player steps on it and deactivate it when the player steps off it requiring the player to find an object in the game world to hold it down, other games have it activate a timer that allows the player to interact with the game world in different ways. Some games have it activate by the amount of weight that is on it. Some games have a combination of all the above. No mater how the pressure pad is activated the basics is pretty much the same.

For this Game the direction that I was given was to activate the pressure pad once the moveable box was fully on it, or at least far enough on it to be fully on it the moveable box should stop moving and some event should happen.

I cache the string of the tag of the object that I want to activate this trigger. I also have a bool for if an activation object has entered the trigger.

In the On Trigger Enter Method I check to see if the object that entered this trigger is the activation object if it is I set the bool to true. In the On Trigger Exit Method I check to see if the object that exited this trigger is the activation object if it is I set the bool to false. In the Update Method I check to see if I have an activation object. If not I exit the method.

I need to know the position that the pressure pad is at. I make sure to cache this in the Start Method. I also need a radius because I the movable object to be mostly on the pad before it is engaged. I also need the transform of the activator object so i get this in the On Trigger Enter Method. I also set it to null in the On Trigger Exit. In the Update Method if I have a activator object then I check its position on the X axis compared to the position of the pressure pad, if it is not with in the radius I exit the method. I could also use Vector3.Distance or a Raycast for this platform.

In Unity I make sure to set the Tag of the Moveable object to Pressure Activator.

If the activator object is close enough I set the has activation object to true so the update logic will not run the next frame. I the get the rigidbody of the activator object. If it has one I set the is kinematic to true. I set the activator transform position to be the position of the pressure pad. I set the activator transform to null.

I create an Unity Event that I Invoke if it is not null.

In Unity I set the Activation Event for the pressure pad to change the Material.

Now I have Create a condition where the player can not get past.

To fix this I set the rotation of the activated object transform to be identity.

Now I have a working pressure pad.

--

--

Excited about changing my hobby into a new carer with GameDevHQ course.