Pushing a box

This article will go over how to push an object.

A few things are needed when thinking about pushing an object.

  1. The object set to push.
  2. The player or object doing the pushing.

As above, the code is in the OnControllerColliderHit method. What the method does is when the PlayerController component is hit by a collider, it sends information. How you use that information is up to what is happening. In this case, we’re using it to push a block. The player runs into a cube that is tagged as a “Pushable” allowing for a way to be identified. The information of the block is stored in the hit variable, and if it’s tagged as a “Pushable” we try to grab the Rigidbody attached to it. If it has one at least. If it doesn’t or it is set to kinematic (which tells it not to allow forces to be applied to it), we don’t do anything. If the object is below us, we also don’t do anything. After all of that we make a new Vector3 variable for the move direction of the object which is just the direction that the player is moving in when it collided with the object. Then we set the velocity of the objects rigidbody to the move direction vector and a push power that was assigned.

Next is getting a pressure plate to trigger with the block on it.

Pressure Plate script

This script is on the pressure plate object. When the block is pushed into the trigger, you check to see if it’s as far as you want. Checking to see if the position inside the trigger is where you’d like it to be for it to activate. Once it’s on the plate as far as needed, you can use that to activate something. A door, a bridge, etc. It’s a distance check between the plate and the object being pushed. You would then set the rigidbody of the object to kinematic for it to stop being moved when you push it.

With that it’s on to the next.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hash Tables: Complete

Next.js Benefits & Features

Zero to Next.js Example with WSL2 in W11

Osome7 customer Care number/858489730//8584892730Osome7 customer Care…

OpenWhisk, Serverless, and Security — a POC

Drawing Basic Charts with React, TypeScript & d3. Part I — Line, Area, and Bar Charts.

Strengthen a React Application with Github and GitHub Actions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brennen Witzens

Brennen Witzens

More from Medium

Retrospective of the 2D Galaxy Shooter

Cinemachine — Under Attack

Learn to code by making a 2D space shooter game.

2D Mobile Game: Tilemaps