Scripting: Teleporting

Step 1
In this lesson we'll learn how to create teleports that we can put on doors or gates! optimize %283%29
Step 2
Open/Load your project from the previous lesson. There should be collecting coins and a player.
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"
Step 5
Open up the TeleportTo script and add the following code into our script.
Step 6
Let's see what this code does...
Step 7
Save your script and go back to Unity. Create an empty object named "Location1".
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.
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.
Step 12
Let's see what these pieces of code do...
Check Your Work
Step 13
Save the script, Go back to Unity, Hit Play and test to see if this works!!

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!!

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.

Let's Delete our MultipurposeCameraRig.


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.
Check Your Work
Step 18
Try it again and you should have more control of the Camera.
Step 19
Can you create red enemies that sends back to the starting position?

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

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

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".
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"

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

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!
Step 27
Let's see what the loadLevel.cs script does...
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.
Step 29
Type "level2" in that slot.
Step 30
Now we want to connect these two scenes. Click File and then "Build Settings..."
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.
Check Your Work
Step 35
To test this, open up your level1, hit play and touch the door.
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


MVCode Clubs

Created By

Nepgear tina (instructor)


Unity Advanced

Access Level


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