Day 109: Show End Scene

Objective: Show end scene after cube is one pressure pad.

We will need set up multiple things for this. We will start with the scene changing. In our Hierarchy we will create a new canvas for the end game scene.

We will add an image for the background which we will change to black and a textmeshPro text. The text will be centered.

Now on the canvas we will create an animator and create a new animation. This animation will just fade in from zero alpha when the object is re-enabled.

Last thing to do is to make sure the Loop Time is not selected. If it was this animation would just keep going as seen above.

Now that we have the animation done, we will disable the game object so we can see the game.

Now we need a way to call this. Since we are using a cube and pressure pad to active the end of the game we will create a cube to be pushed and a pressure pad.

The cube will need a RigidBody on it.

Now to move the cube we will need to go into the player script. We will use the method named OnControllercolliderHit(). This is a method that is used with the CharacterController. In here we will check to see if what we hit was the cube by using CompareTag.

Next is to see if the object has a rigidbody on it. If it does not, we will ignore the cube and not interact with it.

Next is to check to see where we are hitting the cube. If the move direction is less than -3 we will return. We don’t want to push the cube down.

We will need to get the direction the cube will move in. We do this by creating a local Vector3 and assign it the moveDirection from hit. In this game we are moving along the z axis instead of x, the hit.moveDirection will be on the Z axis. Next is to assign the cubes rigidbody’s velocity to pushDir and multiply by push power.

Now we can push the cube. Next will be the pressure pad. We have a empty object that holds the script and a box collider that is set as a trigger. We will have another cube as a child to change color and move down a little.

The pressure pad will do a few things. One we will use OnTriggerStay to check the distance of the cube from the center of the pressure pad.

Once the cube is less than or equal to 1f, we will set the cubes Rigidbody Kinematic to true, this is to prevent the cube from moving anymore.

We will need a global variable to change the color of the cube, this will grab the renderer from the display.

Now back to the OnTriggerStay, after the cube is set to Kinematic we will change the color of the cube. We will also set the position of the cube down .081f. This is to give the appearance of actually having something on it.

This next part is to start the animation of the end scene. For this we will use some unity event. To do this we will need the namespace of UnityEngine.Events.

Next is to create a UnityEvent variable.

Now we will invoke the events.

Now on the pressure pad object we will have event section in the Inspector now. We will drag in the CanvasEndGame and our player. The Canvas we will enable and the player we will disable the charactercontroller so the players can’t move.

Unity event calls what is set in the inspector. There are more uses of Unity Events that can be found here:

That is all there is to enable the end scene with the pressure pad! This could be used to open a gate or more. Only your imagination is the limit.

Thank you for reading and have a wonderful day!

--

--

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
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer