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
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. 😅
thx
Yikes
you Talk and Move like Elon Musk, wtf...
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. 🤣
Oh cool! Didn't know 'yield return null;' would have the same effect. 😀
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.
Could you please explain?
you look like if elon musk made video games. no offence
Thank you so much bro.
I'm working on Mobile Teleportation
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.
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
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! =)
Thank you so much for this awsome tutorial. Ive been having issues for the last 3 hours but you saved me :D
Nice work explaining this, thanks a lot! I'm trying to emulate elevator movement using teleportation. Using delay is a perfect approach.
Thanks I was stuck there that was right very helpful.
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.
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.
It works for me with CharacterController. Thank you very much!
I cant find a tutorial that shows how to teleport a character, when they press a button
"sometimes failed" is why im here
hey I have been learning php recently and your videos are helping me a lot, just wanted to say thank you.
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
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.
Perhaps chatgpt isnt as good as it sounds, thanks for the tut!
Thanks used this to make windows jump 😂
player controller isnt a thing
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.
I will have a video up on that tomorrow 😊
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?
What if teleporting on an empty game object? And only when in an trigger to be able to? You need to add on onColliderEnter?
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.
Thanks Dani. I've been stuck for way too long on this bug in my game.
This has been fixed on latest Unity update. No need to use couroutine.
thx!
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
one question : what i want to teleport an object that i have Instantiate in game ?
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.
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
thanks soooooooooooo much for this video, i have struggle with this function in my game for days!!!! finally solve it
Love the detailed explanation! Is there any chance to make this a lerping/smooth movement instead of an instant?
I was literally stuck on this for 2hours, no being able to teleport my player, you saved me! Thank you!!!
Great Tutorial! thanks.
Genial, hat mir sehr geholfen!!
instead of clicking, is there a way to detect a colision with another object? (e.g: portal)
PS its a vr game
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.
My "how to install Unity" video shows how. :)
thank you so much
Thx so much master!
Hi I am sayed from Bangladesh... I like your videos....
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...
yo the best, I really needed this
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?
Just store your Vector3 at the moment of pressing the button, and then use that as your last teleported location. 🙂
Very useful info!! Thank you so much!
Jesus... so much coding for this
i acidently made a teleport its not that complicated
just take dani grapple gun script and make it a fixed joint instead of spring joint
Can I do this with “SetActive” instead of enabled?
Sure, I can't see why not. 🙂
I loved it
Thank You!!!
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.
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).
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)
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
@@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. .-.