HOW TO TELEPORT OBJECTS IN UNITY 🎮 | Teleport Player and GameObjects in Unity | Unity Tutorial

preview_player
Показать описание
Hi everyone! 🙂 Today I will show how to teleport the player and other objects in Unity. I will also show why it sometimes won't work for your player character and how to fix it.

➤ GET ACCESS TO MY LESSON MATERIAL HERE!

First of all, thank you for all the support you have given me!

I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!

I am now using Patreon and YouTube Memberships to share improved and updated lesson material, and for a small fee you can access all the material either from my memberships or Patreon, depending on your preference. I have worked hard, and done my best to help you understand what I teach.

I hope you will find it helpful :)

Рекомендации по теме
Комментарии
Автор

A friendly commenter on my last video corrected a mistake, where I say that you need to reference scripts inside the Awake(), when it is actually supposed to happen inside the Start(). 🙂 Unfortunately I made these two videos at the same time, so I make this same mistake in this one as well hehe. Whops. 😅

Dani_Krossing
Автор

Got it to work without disabling movement, used wait for end of frame instead of wait for seconds before changing position.
Appreciate the help, spent the whole day tinkering on this.

TheGloriousJeb
Автор

Thanks for the tutorial! You've saved my day!
I'm not very experienced with c# and have spent several hours trying to teleport my player without disabling a controller! =)

SuperOsmaniac
Автор

I love how slow you go through things! As a unity and coding beginner i feel like most tutorial fly past the details and I tend to get lost easily because of it. I will definitely sub and take a look at some of your other tutorials when I get the chance.

duckenomics
Автор

I was literally stuck on this for 2hours, no being able to teleport my player, you saved me! Thank you!!!

Akaimari-kwog
Автор

Hello 😁Nice video, thumbs up!

Since you were wondering how fast you can execute the coroutine; What you could do as well is 'yield return null'. That will wait for one frame and it will be enough to teleport the player.

It might be of interesst why exactly that behaviour is happening at all and that little mystery I think I can explain. The reason is that the CharacterController component stores the position of the object in its own field and applies that position to the position field of the Transform component on every frame, basically overwriting the teleport input since the only official way to alter the position of the CharacterController internally is by using the Move() or MoveSimple() method of the CharacterController itself.
Every time the CharacterController gets enabled it reads the position of the Transform component, therefore refreshing/overwriting the previous position stored. So technically it should be enough to only disable the CharacterController component itself, then teleport and after that enable it again. Might be preferential if you don't want to restart other scripts on the player object.
Why exactly you weren't able to do all of it in the same frame I'm actually not sure about, might be some conflicht with the PlayerController script itself.

Since I'm a lazy typer, what I do is using a little extension method for the CharacterController itself:

public static class CharacterControllerExtensions
{
/// <summary>Moves the character controller (Transform) to specified position.</summary>
/// <param name="position">Target position</param>
public static void TeleportTo (this CharacterController controller, Vector3 position)
{
controller.enabled = false;
= position;
controller.enabled = true;
}

/// <summary>Moves the character controller (Transform) by specified vector.</summary>
/// <param name="vector">Vector to be moved by</param>
public static void TeleportBy (this CharacterController controller, Vector3 vector)
{
controller.enabled = false;
+= vector;
controller.enabled = true;
}
}

Why unity itself isn't including functionality for that out of the box I don't understand. It's a hack as well, obviously, but it's a lot less typing and way less cluttered code. Extensions might be a bit far of for the tutorial series, maybe, but I thought I share this with you guys anyway.
Another solution could be overwriting the position field of the CharacterController directly using System.Reflection, but that's way to advanced stuff for the tutorial series at this point. 🤣

MatthiasGut
Автор

Thank you so much for this awsome tutorial. Ive been having issues for the last 3 hours but you saved me :D

real_unreal
Автор

I know exactly zero about coding or programming and I have no idea what I just watched...but I still enjoyed it. Two thumbs up and a "like" lol

superkitten
Автор

Nice work explaining this, thanks a lot! I'm trying to emulate elevator movement using teleportation. Using delay is a perfect approach.

lobbywww
Автор

Gosh Dani I've been using playmaker to set the player's position but continuously face the same 'teleport issue' you mentioned after deactivating the player everything is going well. You help me a lot really really thanks.

zengtian
Автор

It works for me with CharacterController. Thank you very much!

КибершколаСмарторика
Автор

Thank you so much bro.

I'm working on Mobile Teleportation

creepermods
Автор

hey I have been learning php recently and your videos are helping me a lot, just wanted to say thank you.

vraj
Автор

Thanks Dani. I've been stuck for way too long on this bug in my game.

lukaspaver
Автор

Thanks I was stuck there that was right very helpful.

GhostSy_
Автор

thanks much for this video, i have struggle with this function in my game for days!!!! finally solve it

eznogamee
Автор

Thank you, disabling the rigidbody with this method made my Teleport Skill work each time now. I was baffled what's the reason for it only working sometime.

ggshout
Автор

Hi I am sayed from Bangladesh... I like your videos....

sayed_technical
Автор

Thanks for the video, it did help a little with one issue I had.
Perhaps you could make something like this video that deals with multiple teleport spots?
For example A -> B with option to go to C, D, or E from a list. IF you have the time, willingness and skills to do so that is. Not found anything like this out there yet.

_SF_Media
Автор

Perhaps chatgpt isnt as good as it sounds, thanks for the tut!

RizevimVanZ