In this step, we will create the finish point, which is the door in this case. We will also create a Trigger Collider
, which makes it so that the player can't end the game if he/she didn't collect our item; of course it's the key to our door.
Let's prepare and make sure that we have all the graphics that we need; go to the Graphics
folder in the Project
window, and make sure in our subfolder Level
, we have doorClose.png
, doorOpen.png
, and key.png
. Let's get start.
Here, we will create the object's Key
and Door
. Let's do this as follows:
M_Key
, and set the following:key.png
in the Graphics/Level
to the texture thumbnail in the material inspector:doorClose.png
in the Graphics/Level
to the texture thumbnail in the material inspectorTag
for our Door
and Key
, so go to Edit | Project Settings | Tags.Player
. So, go to the Plane
prefab object in the FBX
folder and drag it into the Hierarchy view.Plane
prefab object, and choose Rename to change the name to Key.Player
object.M_Key
to this material. Then add the Box Collider
to the Key
as we did for our Player
; go to Component | Physics | Box Collider set Size: x: 1, y: 1, z: 1 and Center: x: 0, y: 0, z: 0, and toggle Is Trigger to true.Key
by pressing Command + D or Control + D to create the Door
object. Then, we name it Door
, assign material M_Door
to it, and set the following:Door
and Key
. Next, we will go back to our code and add some scripting to make our Door
and Key
work. Double-click our CharacterController_2D.js
, and add these parameters to it:public var doorOpenTexture : Texture2D; public var doorCloseTexture : Texture2D; private var b_hasKey : boolean;
Start()
function://Start with no Key b_hasKey = false;
This will set the character to start without a key.
OnTriggerEnter()
function to our code; this function will check if our character hit Key
or Door
:public function OnTriggerEnter (hit : Collider) : IEnumerator { if (hit.collider.tag == "Key") { if (!b_hasKey) { //We hit our Key b_hasKey = true; Destroy (hit.gameObject); } } if (hit.collider.tag == "Door") { if (b_hasKey) { //If we had Key and hit door the door will open hit.gameObject.renderer.material.mainTexture = doorOpenTexture; //wait for 1 second and destroy our character yieldWaitForSeconds(1); Destroy (gameObject); //We close the door hit.gameObject.renderer.material.mainTexture = doorCloseTexture; } } }
In this function, we are checking if our character hit the key
or door
by checking their tag. When the player hits the key
, the key will destroy itself and we set our character to have a key by setting b_hasKey = true
. Also, when we hit the door
, we are checking if our character has the key or not. If the character has the key, it will change the door texture to doorOpen
texture. Then, we wait for one second to remove our character and we change the door texture back to doorClose
texture to close the door.
doorOpen.png
and doorClose.png
to the Player
. Go back to Unity, and click on the Player
; in the Inspector view now, we will see two new parameters, Door Open Texture and Door Close Texture; drag-and-drop doorOpen.png
to Door Open Texture and doorClose.png
to Door Close Texture. Now we are done.Click play and try out your game, collecting the key and going to the door. Behold the door opening and closing!
We just created a key
and door
object, and placed them at our level. We also created the function that will trigger when the character hits the key
and door
objects. Then, we changed the texture of our door
object when our character had a ke
y object and hit the door. Lastly, we waited for one second to remove our character from the scene and changed the door
texture back to closed state by using yield
and Destroy
.
We can pause or wait for the next action by using coroutines
.
In our script, we need to wait for a second between opening the door and ending the game. We could do this by looping or performing some other task for a second, but that would stop the animations, the sound, and everything else. We get around this by using the yield
command; this tells Unity to stop running our function and come back later (in our game, 1 second later as we call yield WaitForSecond(1)
). By using the yield
command our function becomes Coroutines
and now it must return IEnumerator
(Unity needs this so that it can tell when to start our function again). This means Coroutines
can't return a value like a normal function. We can change most functions in our MonoBehaviours
script into Coroutines
, apart from the ones which already run in every frame, such as Update()
, FixedUpdate()
, OnGUI()
, and so on. We can get more information about coroutines
from the following Unity script reference:
http://unity3d.com/support/documentation/ScriptReference/Coroutine.html.
Next, we will talk about the return type. Sometimes, when we use JavaScript, we don't really care about what type to return or what type of parameters we will pass to the function, because it is really convenient to type only var myParams = 0
or function DoSomething(var)
. This isn't a bad thing to do, but if we are working with a team of people, it is very important to have code that is readable for others. So, it is better to have this habit. It also makes the code run faster, since it doesn't have to go and do type lookups. On the other hand, if we use C#, we will be forced by the language itself to type the return type of this function or the type of this parameter. So, it's a good thing to know because you will be able to read C# code easily if you have to and it is readable for everyone, even the person using C#.
3.15.235.188