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

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024
  • 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.
    Learn C# here: • 1: Introduction To C# ...
    Download Unity here: unity3d.com/ge...
    ➤ 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 RUclips 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 :)
    Memberships: / @dani_krossing
    Patreon: / mmtuts

Комментарии • 70

  • @Dani_Krossing
    @Dani_Krossing  2 года назад +18

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

  • @PlaceholderNameRandomNumber
    @PlaceholderNameRandomNumber 2 года назад +3

    you Talk and Move like Elon Musk, wtf...

  • @MatthiasGut
    @MatthiasGut 2 года назад +10

    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
    {
    /// Moves the character controller (Transform) to specified position.
    /// Target position
    public static void TeleportTo (this CharacterController controller, Vector3 position)
    {
    controller.enabled = false;
    controller.transform.position = position;
    controller.enabled = true;
    }
    /// Moves the character controller (Transform) by specified vector.
    /// Vector to be moved by
    public static void TeleportBy (this CharacterController controller, Vector3 vector)
    {
    controller.enabled = false;
    controller.transform.position += 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. 🤣

    • @Dani_Krossing
      @Dani_Krossing  2 года назад

      Oh cool! Didn't know 'yield return null;' would have the same effect. 😀

  • @TheGloriousJeb
    @TheGloriousJeb 2 года назад +5

    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.

  • @alex_ig_
    @alex_ig_ Год назад +1

    you look like if elon musk made video games. no offence

  • @creepermods4641
    @creepermods4641 Год назад +1

    Thank you so much bro.
    I'm working on Mobile Teleportation

  • @duckenomics7981
    @duckenomics7981 7 месяцев назад +2

    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.

  • @tLr1n
    @tLr1n 2 года назад +1

    what is UpdateMouseLook and UpdateMovement at 6:20? Is it just your movement and mouse look scripts with the word update in front of them? Because when i do the same thing it says it doesn't exist

  • @SuperOsmaniac
    @SuperOsmaniac 7 месяцев назад +1

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

  • @real_unreal9644
    @real_unreal9644 7 месяцев назад +1

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

  • @lobbywww
    @lobbywww 23 дня назад

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

  • @GhostSy_
    @GhostSy_ 6 месяцев назад +1

    Thanks I was stuck there that was right very helpful.

  • @ggshout3019
    @ggshout3019 2 месяца назад

    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.

  • @zengtian9451
    @zengtian9451 2 года назад +1

    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.

  • @КибершколаСмарторика
    @КибершколаСмарторика 3 месяца назад

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

  • @Omnivoid22
    @Omnivoid22 8 месяцев назад

    I cant find a tutorial that shows how to teleport a character, when they press a button

  • @StickyLabDev
    @StickyLabDev 6 месяцев назад

    "sometimes failed" is why im here

  • @vraj2003
    @vraj2003 2 года назад +1

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

  • @JuliaEsAwesome
    @JuliaEsAwesome 2 года назад +1

    Ik this is off topic of your video but do you know how much you will charge to check over someone ecommerce website-and let them know what they need to fix and add

    • @Dani_Krossing
      @Dani_Krossing  2 года назад

      I don't do that type of service :) And to be honest I'm more of a front end developer than a back-end developer, so I wouldn't be able to help if it came to it.

  • @RizevimVanZ
    @RizevimVanZ 9 месяцев назад

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

  • @deadevil_fst
    @deadevil_fst 5 месяцев назад

    Thanks used this to make windows jump 😂

  • @PerplexingRat
    @PerplexingRat 2 месяца назад

    player controller isnt a thing

  • @_SF_Media
    @_SF_Media 2 года назад +1

    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.

    • @Dani_Krossing
      @Dani_Krossing  2 года назад +1

      I will have a video up on that tomorrow 😊

  • @dertobbe1176
    @dertobbe1176 Год назад

    Hi, I made some kind of UT 2004 transcolator. I just said player.position = transcolator.position... It Kanada works. But not always. My guess is that's it's about the right frame... How can I be sure to Set the New Position to the translocator Position no matter what?

  • @heykuze
    @heykuze Год назад

    What if teleporting on an empty game object? And only when in an trigger to be able to? You need to add on onColliderEnter?

  • @zoomiee6229
    @zoomiee6229 2 года назад

    How could i do this with a "OnTriggerEnter(Collider other)" method? Because im trying to do this with a vr game. Im just going to send my code here with what i have and if you can tell what I can do to fix it because I get a double float error.

  • @lukaspaver
    @lukaspaver Год назад

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

  • @lotfitej8101
    @lotfitej8101 Год назад

    This has been fixed on latest Unity update. No need to use couroutine.

  • @pulinho5451
    @pulinho5451 6 месяцев назад

    thx!

  • @thewaterwasfine
    @thewaterwasfine 9 месяцев назад

    spent the past 4 hours wondering why i couldnt get my player to teleport then came across this godsent video. unfortunately 0.01f is too fast for OnTriggerEnter teleporting so i think im just going to have to make a physics based playercontroller

  • @patelvraj1362
    @patelvraj1362 Год назад

    one question : what i want to teleport an object that i have Instantiate in game ?

  • @Stoney_Eagle
    @Stoney_Eagle 2 года назад

    I don't have Danish translations implemented yet so I thought I just do one where it matters the most to me...
    Howcome a 3-word sentence is wider in pixels in Danish than the English version even tho they are the same character length? 😂😅😭
    It's that crazy æ symbol I guess, my dynamic font size calculation doesn't work because of it... It's a good thing I see it now before release.

  • @superkitten7560
    @superkitten7560 2 года назад

    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

  • @eznogamee3704
    @eznogamee3704 2 года назад

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

  • @u2bt257
    @u2bt257 Год назад

    Love the detailed explanation! Is there any chance to make this a lerping/smooth movement instead of an instant?

  • @Akaimari-kw8og
    @Akaimari-kw8og Год назад

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

  • @amitbt69
    @amitbt69 Год назад

    Great Tutorial! thanks.

  • @Graviteam
    @Graviteam Год назад

    Genial, hat mir sehr geholfen!!

  • @Ryzen4070TM
    @Ryzen4070TM Год назад

    instead of clicking, is there a way to detect a colision with another object? (e.g: portal)

  • @legoose
    @legoose 2 года назад

    Hi, Visual studio noob here. Would you kindly inform me how to turn on (not 100% sure what it's called) the auto complete.
    I've been looking at visual studio for the last two hours and still haven't found the settings.

    • @Dani_Krossing
      @Dani_Krossing  2 года назад +1

      My "how to install Unity" video shows how. :)

  • @rudikrasna6846
    @rudikrasna6846 Год назад

    thank you so much

  • @gmangman123
    @gmangman123 Год назад

    Thx so much master!

  • @xstmlao_technical
    @xstmlao_technical 2 года назад

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

    • @xstmlao_technical
      @xstmlao_technical 2 года назад

      i want to contact you.. please give me your contact address.. if you're set your WhatsApp number, then please give me your WhatsApp number please brother...

  • @SteadyPaceVince
    @SteadyPaceVince 2 года назад

    yo the best, I really needed this

  • @darkman237
    @darkman237 2 года назад

    Now if you wanted to go back to the first plane and end up at the same place you hit the button, how would that be accomplished?

    • @Dani_Krossing
      @Dani_Krossing  2 года назад

      Just store your Vector3 at the moment of pressing the button, and then use that as your last teleported location. 🙂

  • @i1234pipio
    @i1234pipio 2 года назад

    Very useful info!! Thank you so much!

  • @canalataquerapido
    @canalataquerapido 2 года назад +1

    Jesus... so much coding for this

  • @3th1nhawn50
    @3th1nhawn50 Год назад

    i acidently made a teleport its not that complicated

    • @3th1nhawn50
      @3th1nhawn50 Год назад

      just take dani grapple gun script and make it a fixed joint instead of spring joint

  • @zoomiee6229
    @zoomiee6229 2 года назад

    Can I do this with “SetActive” instead of enabled?

  • @regys9521
    @regys9521 2 года назад

    I loved it

  • @FadilSyhptra
    @FadilSyhptra 2 года назад

    Thank You!!!

  • @johanneswagenknecht737
    @johanneswagenknecht737 2 года назад

    Hey @Dani Krossing,
    I was wondering what the sign after playercontroller in line 12 is? Please help me because im going to fail IT class.

    • @Dani_Krossing
      @Dani_Krossing  2 года назад

      One of the most useful and often used skills of any IT person, is Google. 🙂 If you search "What are the symbols called?" then you will find your answer. It took me 20min to see your comment and reply, where as a Google search takes 10sec. 😉Not trying to sound snarky hehe (maybe a little).

  • @devonlewis444
    @devonlewis444 2 года назад

    okay, so Is there a way to teleport a specific distance? instead of to a certain object? because I'm making a game where a monster is at the end of a hall way, teleporting towards you every 5 seconds or so, and I was wondering if there was a way to make it move a specific distance, and maybe to randomize the location of where it teleports to in between the walls.
    (sorry, I know that might be a lot, but I'm kinda lost lol)

    • @Dani_Krossing
      @Dani_Krossing  2 года назад +1

      You can create a Coroutine that runs a function every 5sec, and then inside the function you get the direction towards the player using a normalized offset of the player and the monster, and just add the distance you want it to teleport. 🙂
      This video can help you getting the offset: ruclips.net/video/30_spR3cCxw/видео.html&ab_channel=DaniKrossing

    • @devonlewis444
      @devonlewis444 2 года назад

      ​@@Dani_Krossing Update, I've figured out the code. XD My only issue now is that I used a Random.Range for the x axis, so that it would move around the hallway when it teleported. But the range I set moves with the object, so sometimes it teleports outside of the hallway. and I have no idea what a solution would be to that. .-.