Scripting: Teleporting

Planning
Step 1
In this lesson we'll learn how to create teleports that we can put on doors or gates!
Ezgif.com optimize %283%29
Step 2
Open/Load your project from the previous lesson. There should be collecting coins and a player.
Temp1545605536
Challenge
Step 3
Let's create a door! Create a cube with a brown colored material, and a door knob attached to it.
Step 4
Select the door and add a new script called "TeleportTo"
Temp1545606556
Step 5
Open up the TeleportTo script and add the following code into our script.
Temp1545607830
Information
Step 6
Let's see what this code does...
Temp1545607884
Step 7
Save your script and go back to Unity. Create an empty object named "Location1".
Temp1545607346
Step 8
Select your door and click & drag your "Location1" onto the target placeholder in the TeleportTo.cs script.
Check Your Work
Step 9
Trying moving the door and Location1 around, you'll see that they will stay connected by the 3D white line.
Information
Step 10
If we press play and try touching the door we don't teleport yet, but we do have the location set.
Step 11
Go back to the TeleportTo.cs script. And add these lines.
Temp1545608984
Information
Step 12
Let's see what these pieces of code do...
Temp1545609163
Check Your Work
Step 13
Save the script, Go back to Unity, Hit Play and test to see if this works!!
Temp1545609351

NOTE: before you hit play, if you select the door, you'll see that there's an empty place for player. This is normal because when you press Play, it should automatically fill in our Player.

We see our Player empty slot on the TeleportTo script has been filled with our player after we hit play!!

Challenge
Step 14
Create a series of Teleporting doors!

NOTE: We always want our TeleportTo script on our door. So... We should have the door as a prefab so every time we create it, it'll always have the TeleportTo script.

The player placeholder will always be our player, but our "target" placeholder will always be different. Make different empty object locations for each door.

Step 15
Important: From now on we'll need to manually look a round and move fast. Let's adjust our character & Cameras.
Temp1545686402

Let's Delete our MultipurposeCameraRig.

Temp1545686532

In our Assets/Standard Assets/Cameras/Prefabs folder, Click and drag the FreeLookCameraRig into our Hierarchy.

Check Your Work
Step 16
Press play and check to see if it works. The Camera moves too slow and the player sometimes move out the Camera.
Step 17
You can Adjust the FreeLookCameraRig's Move and turn speed. I have mines at 3 for move and 5 for turn.
Temp1545686916
Check Your Work
Step 18
Try it again and you should have more control of the Camera.
Challenge
Step 19
Can you create red enemies that sends back to the starting position?
Temp1545687227

Just like our JavaScript game Design but in 3D!!

Temp1545687937
Step 20
Next let's try scripting how to move from one Scene to the next! Create a door/portal object!
Temp1545691104

I used the Magic ring prefab from this Particle asset for my portal object. (4.7mb)

Step 21
Select your next level door object and add a new script called "loadLevel".
Temp1545786987
Step 22
Now let's create the next level! Make sure you save your current scene and then make a new scene.

Save and name your new scene as "level2"

Planning
Step 23
Use your imagination to create your second level! You can make another terrain or try something new!
Temp1545787417

Make sure that your level2 scene has:
* A player
* FreeLookCameraRig
* Something that the player can stand on.
* The UI Canvas that you made.

I used this Town asset

Download Town.unitypackage
Check Your Work
Step 24
Hit play and make sure your level2 scene works.
Step 25
Go back to your level1 scene and now open up the loadLevel.cs C# Script that you made.
Temp1545788625 Temp1545788638
Step 26
Type these code segments into your loadLevel.cs script!
Temp1545789267
Information
Step 27
Let's see what the loadLevel.cs script does...
Temp1545789492
Check Your Work
Step 28
Go back to your Unity project, select your next level door and you now see a space for typing the load scene name.
Temp1545789752
Step 29
Type "level2" in that slot.
Temp1545789890
Step 30
Now we want to connect these two scenes. Click File and then "Build Settings..."
Temp1545789982
Step 31
Click "Add Open Scenes" to add your current opened Scene. It should be level1.

Note: I named my level1 scene as "test".

Step 32
Now let's add your level2! Click File and Open Scene, and open your level2 Scene.
Temp1545790192 Temp1545790224
Step 33
With your level2 Scene opened, Click File and Build settings. Add this open Scene too.
Temp1545790291 Temp1545790316
Check Your Work
Step 34
So now we have both level1 and level2 Scenes in the build.
Temp1545790381
Check Your Work
Step 35
To test this, open up your level1, hit play and touch the door.
Challenge
Step 36
Try to create a 3rd level!
Step 37
You are finished with this lesson! Click Submit and go onto the next lesson on Enable/Disable!
Scripting: Teleporting Info

Account

MVCode Clubs

Created By

Nepgear tina (instructor)

Course:

Unity Advanced

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach