#unityloadscene #unityenterdoor
In this video I show you a simple way to make your character enter a door and load another scene by pressing Enter button on your keyboard.
Needless to say that this is just a classic. I've got one.
Logitech HD Pro Webcam C920
https://amzn.to/3lDe6sr
TO BLAST! - My New Fun Relaxing Puzzle Game Available On Google Play Store
https://play.google.com/store/apps/details?id=com.ZoGames.ToBlast
Guess The Movie Is Available On Play Market For Free For Android devices
Here is the link
https://play.google.com/store/apps/details?id=com.zogames.GuessTheMovie
Consider purchasing my ULTIMATE UDEMY COURSE with a great discount https://www.udemy.com/how-to-make-games-with-unity-software/?couponCode=14-99-SALE-GOOD-LUCK
If you like what I'm doing then you can support me through
www.patreon.com/alexanderzotov
or here
https://www.paypal.me/AZotov
Or You Can Become A Sponsor Of My Channel
https://www.youtube.com/channel/UCYgUFrFyfr5IyL8HfTTiWhA/join
Thank you :-)
Project Files Here https://drive.google.com/file/d/1-MjL_7_FcdBMEQ_Yw3CpAL-cqxWXadYp/view?usp=sharing
Let me start from an empty project and quickly set it up. First I create a new scene and rename it as Level 1. Then I duplicate it by pressing Ctrl+D keys and rename this new scene as Level 2. To make it possible to load any scene I need to add it to the build. File - Build settings - select these two scenes and D&D them into this window. Select level one. I need a character to play with. Here it is. Set Order in layer to 5 for example so the character appears above the door. I add rigidbody 2d component that will help my to move my character and detect collisions. Set gravity scale to zero so character will not fall down. Next component will be a box collider 2d. Set it as a trigger and edit it a bit. This collider will detect doors. My character needs a script to move. Here it is. It’s a simple script that allows me to move my character left and right. Script goes to my man. Another script named Enter door helps my to detect doors and enter them by pressing Enter key. So when my character collides with a door ontrigger enter 2d method is invoked. If my man collides with blue door then level 2 should be loaded. If he collides with brown door then level 1 should be loaded. Enter allowed variable is needed to let my man enter a door only if he collides with it. On trigger exit is invoked when my man exits door collider. When it happens enter allowed is set to false. And in Update method I check if enter is allowed and if enter key is pressed at the same time. If those conditions are met then the scene is loaded. Script also goes to man. This character will be spawned every time a new scene is loaded. So to make it possible I need a prefab of my man. D&D him to project folder to create a prefab. This man from hierarchy can be removed. Now I create a door. Here is the blue one. Door needs a collider. Set it as a trigger. Edit it a bit. Each door will be spawning my character. So this Spawn man script will help me with that. So when a scene is loaded a new man is instantiated at the door's position. Serialize field helps me to assign man prefab in the inspector. I’ll do it later. Script goes to blue door. Also blue door will have blue door script which is just empty one that helps my man to identify this door as blue one. Script goes to blue door as well. And finally I add a simple UI text to the scene that just tells me that this is level 1. Level 1 is ready. Save it and select level 2. This level is the same as the first one. So I do the same steps to set it up. Add brown door. Add collider. Add spawn man script and add brown door script which is also an empty one. And add a simple text. The last thing to do is to assign that man prefab to spawn it. Select door and D&D man prefab to this man slot of spawn man script. Save level 2. Select level, select blue door and D&D man prefab to man slot. Save the scene. Now I can hit play and see how it works. I can move my character left and right and step closer to the door and hit enter then the corresponding scene is loaded.
0 Comments