Code Snippets

trenton hodge
Aug 28, 2017 · 5 min read

For the above code titled “MovingFloor”, its overall function is to make a certain game object move right while the player remains in a certain play area (trigger).

Lines 1–3) Make references to the libraries used in this class.

Line 6) Establishes the name of the class.

Line 8) Creates a public variable of type GameObject called MovingFloorOne. This particular object will be the first actual moving floor in the game.

Line 9) Creates a public variable of type GameObject called “WWall”. “WWall” stands for “West Wall” referring to the west wall of the play area that moves.

Line 11) Calls the OnTriggerEnter method with a Collider parameter called other. This refers to an in-game Trigger that will be given a code to run when the player enters it.

Lines 12–14) An If Statement that will make “WWall” appear when the player enters the trigger on MovingFloor.

Line 12) The establishment of an If statement that tells the computer to check for a gameObject with the tag “Player” as a condition. It can be read as, “If the game object that just entered the trigger is labeled with tag “Player”, run the following code”.

Line 13) Refers Directly to the game object named “WWall” and calls a method that will make it appear in the game if the before explained condition is at any point set equal to true.

Lines 17–23) Calls a method called OnTriggerStay() that will run a certain code as long as the player remains inside of the Trigger.

Line 17) Establishes the method OnTriggerStay() with a Collider parameter called other.

Line 18) The establishment of an if statement with a condition that checks for the game object with tag “Player”. Can be read as “If the foreign object that is inside the trigger has the Tag “Player”, run the following code.

Line 19) A code that takes the transform component of MovingFloorOne and gets its position before telling the computer on the other side of the += sign to change its position property to move the game object right. It is multiplied by Time.deltaTime to make it update once per second (to make it move smoothly) which is also multiplied by 5 to make it update 5 times per second ultimately setting the speed of the moving floor.

Line 20) Does the same thing as the code on line 19 except in reference to the game object “WWall”.

The Above code is there to stop MovingFloorOne from moving at a specific point while keeping one wall moving until it lines up with the walls of the new play area which ultimately force the player into the new play area.

Lines 1–3) Tell the computer which libraries are in use.

Line 5) Declares a class named FloorStopper.

Lines 6–8) Declare public GameObject Variables called MovingFloorTriggerOne (refers to the trigger on MovingFloorOne), EWall (East wall of MovingFloorOne), and WWall (West wall of MovingFloorOne).

Lines 12–17) Runs a code that will make the floor stop when the MovingFloorOne hits a certain trigger.

Line 12) Calls method OnTriggerEnter with Collider parameter called other.

Line 13) Establishes an If statement with a condition that tells the trigger to check for a game object with the tag “MovingFloorOne”. It can be read as “If the foreign game object has a tag called “MovingFloorOne”, run the following code.”

Line 14) Makes a reference to MovingFloorTriggerOne and sets its SetActive property to false. This will make the MovingFloorTriggerOne disappear and no longer be present in the game scene.

Line 15) Makes a reference to the game object “EWall” and sets its SetActive property to false. This will make the MovingFloorOne’s East wall disappear from the game.

Lines 18–23) Calls the method OnTriggerStay() that tells the computer to run a code as long as the foreign collider remains in the trigger.

Line 18) Calls the method OnTriggerStay() with Collider parameter called other.

Line 19) Declares an If Statement that will tell the computer to check for a foreign game object with the tag “MovingFloorOne”. It can be read as,“If the collider of the other object refers to the game object with the tag “MovingFloorOne”, then run the following code.”

Line 20) Makes a reference to WWall’s transform component and position property and tells to change the position property to make the object move to the right. The Time.deltaTime serves the same purpose as explained earlier except this time multiplied by 3 to make this particular object move a little slower.

Lines 1–3) Tells the Computer which libraries are in use.

Line 5) Declares a class named FloorStopper2.

Line 7) Declares a public GameObject variable named WWall

Line 8) Declares a public GameObject variable named Stopper

Line 9) Declares a public GameObject variable named MovingFloorOne

Lines 11–18) An OnTriggerEnter function that will ultimately tell the before mentioned WWall that was set to keep moving to stop, and the before mentioned MovingFloorOne and all of its child objects to disappear.

Line 11) Calls OnTriggerEnter() method with Collider parameter called other.

Line 12) Declares an If statement that tells the trigger to check for another game object with the tag “WWall”. Can be read as “If the other game object collider refers to a game object with the tag “WWall”, run the following code.

Line 13) Declares a new BoxCollider called WallStopper by referencing the GameObject (trigger) called Stopper and getting its BoxCollider component.

Line 14) References the BoxCollider called WallStopper and sets its “Is Trigger” property to false.

Line 15) References the game object called MovingFloorOne and sets its “Set Active” property to false making the MovingFloorOne GameObject disappear.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade