Doors in Unity - Sliding and Rotating Doors | Make Them Automatically Open/Close or Usable

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
does your game need doors should they rotate to open should they slide to open should they automatically open whenever the player comes nearby all those are great questions to consider in the design phase in this video we're going to take a look at how to implement every one of those hey chris here from mom academy here to help you yes you make your game dev dreams become a reality today with doors you know doors are just all over the place if you're talking about a real world environment you're gonna have some doors it's not always immediately clear though how to make doors work well in your game if you model them to be super realistic where most of the doors in real life only open one way then you end up in some situations where the door opens towards the player or the user and like smacks in the face or pushes them out of the way and then making the player avoid that makes it even more complicated in this video we're gonna make two kinds of doors we're gonna make rotating doors and we're gonna make sliding doors with both of these we're gonna have the option to have the doors automatically open when the player becomes within some range or within a trigger of that door with the rotating doors that will also have the benefit of being able to choose whether we want the door to open realistically where it'll hit you in the face sometimes or if we're going to open it away from the player every time which i personally find to make the game flow a little bit better than the realistic doors with the concepts that we're implementing today you can implement any kind of door that you need for your game hey and just really quick i want to give a shout out to my patreon supporters i really appreciate it every bit helps the channel grow reach more people and add value to more people and that means more people are making their game development dreams become a reality if you want to help me in that cause you can show your support on patreon patreon.com academy you can get your name up on the screen you can get a voice shout out starting at the silver tier and some other cool perks special shout outs to raphael and andrew bowen for being the silver tier supporters i am so grateful thank you in this scene i've already set up a lot of what we're going to do since we've upgraded our unity version to 2020.3.20 f1 we can use the new starter assets for a first person controller and we're using that so that way we can move around in the scene so what we're going to do is implement two kinds of doors sliding and rotating doors we're going to start with rotating doors that we have to press a button to interact with then we'll implement it where the doors will automatically open whenever we come into a range once we've made all the rotating doors work we'll start taking a look at the sliding doors which will again work by pressing a button or by just entering in some range this is the standard player capsule that the starter assets give us so i'm not going to go over too much of what this does because honestly i haven't looked into it too much it just lets us move around and on the inputs because this uses the new input system what we have to do is define a new action that we're going to call use set up with a binding i'm going to choose e because i played counter-strike and that's what used was in counter-strike and set it to be on the keyboard and mouse control scheme because the player input has the behavior set as send messages we see that a new event on use will get sent to any scripts that are attached to this behavior and i've already created the scripts in this case i've created a door a door trigger and player actions so in player actions later we're going to define a function called on use which will get automatically called whenever we press e first things first let's start with the door we'll define a public bool is open so that the false by default a private serialized field bool is rotating door set that to be true by default because we're not doing sliding doors yet a private serialized float speed set that to 1f by default that'll be how quickly the door will open and close i'll add a header called rotation configs because again later we're going to do some sliding configs and we want to keep them a little bit separate in the inspector we'll do a private serialized float rotation amount set that to 90f by default this will be how much the door should rotate to open and a private serialized float forward direction sets it to be zero by default this is going to be the number that we compare our dot product to and we'll talk about how that works in just a little bit we'll have a private vector3 start rotation that we're going to assign on a wake a private vector 3 forward that we will also assign on awake and a private co-routine animation code routine on awake we'll do start rotation equals transform.rotation.euler angles and we'll also assign forward to be transform dot right that might sound a little bit weird but if we take a look at these doors the way that they're oriented forward on them is actually pointing straight into the door frame so if we count that as forward then it's actually impossible to tell which side of the door the player's on so in this case i'm assigning it to be the right side so if the player's on this side then we'll know that that is forward and we should maybe open away from the player next up let's define a public void open that accepts a vector 3 user position the first thing we'll do is check if the door is not already open if it is already open we don't want to do anything we'll then stop the animation curvaturing if it's not null and check if this is a rotating door if it is a rotating door then we'll get the dot product doing float dot equals vector 3 dot passing in the forward and taking a user position minus transform position and normalizing that vector then we'll log the dot product so we can see what it is for some explanation purposes here in just a second and then animation curve routine equals start creatine passing in do rotation open passing in the dot that we just calculated and to give just a really quick explanation what the dot product does is given the particular forward that we passed in here the second vector that we pass in which we're using the normalized direction towards the player this will give us a number between negative one and one indicating where in relation to this forward the player is this is really useful because negative one means they're directly behind this forward and one means they're directly in front of this forward this allows us to do exactly what we talked about earlier is determine whether the player is in front or behind the door and then we can open the door away from the player we'll then define a private eye numerator do rotation open that accepts a float forward amount that's going to be that dot product that we just calculated in there we'll define a quaternion start rotation and set that to be the transform.rotation we'll also define a quaternion end rotation and we're going to wait to assign that a value because we're first going to check if the forward amount is greater than or equal to the forward direction if it is then we're going to assign the end rotation be quaternion.euler passing out a new vector 3. i'm passing 0 for the x and the z because my doors are always standing up straight if you want to have your doors at weird angles you can also use the start rotation dot x and z instead of 0 here then for the y we'll use start rotation dot y and subtract out the rotation amount and use zero for the z if the border amount is less than the forward direction then we're gonna do end rotation equals quaternion.euler passing in a new vector three using zero again for the x the start rotation dot y plus the rotation amount this time and zero for the z because of what we just talked about with the dot product that means that the door will open away from the player if we set the forward direction to be negative one then the door will always open with a negative rotation amount if we set the forward direction be greater than one then we will always open the door with a positive rotation amount now that we have the start and end rotation our two targets let's set is open to be true so that way immediately this door will allow us to close it as soon as we've opened it we'll define a float time to be 0 and say while time is less than 1. we'll assign the transform.rotation to be quaternion.slurp that's just spherical alert but makes it a little bit more interesting than a straight lerp it works the exact same though we'll pass in the start rotation the end rotation and then the time over the period of zero to one on time we're gonna go from the start rotation to the end rotation we're going to yield return null so we wait for the next frame and then do time plus equals time dot delta time times speed that way we can make the door open faster or slower by controlling the speed so this will open our door the next thing we're going to do is do the close because we'll get the door that can only be opened for the close we're going to do almost the same thing we don't open where we first check if it is open we'll then check if the animation curtain is not null so if it's running we'll stop it we'll then check if this is a rotating door if it is then we're going to do animation curtain equals start corrupting do rotation close and for do rotation close we'll do another private enumerator do rotation close and we're gonna do again almost exact same thing we did on open quaternion star rotation equals the current rotation that's transform dot rotation and the end rotation this time is going to be the actual start rotation so we're gonna do quaternion end rotation equals quaternion euler passing in the start rotation then we say is open is false define at float time time equals zero and say while time is less than one transform dot equals quaternion.slurp again passing in the rotation the end rotation and the time we're then going to yield return null and assign time plus equals time dot delta time times speed next up let's do the player actions in here what i'm going to want to happen is have some text show up wherever the player is looking if they're hitting a door that they can open that way we're going to prompt the player to press e to open that door if it's a usable door so i'm going to define a private serialized text mess pro use text a private serialized transform the camera a private serialized float max use distance set to b5f by default and a private layer mask that's also serialized called use layers that way we can choose to only raycast on some specific layers then we'll define the public void on use which is going to be magically called by the new unity input system because we've created a new input called use and that will get triggered whenever we press the e button so in here whenever the user presses e we're going to do if physics.raycast camera.position camera.forward passing out a raycast hit the max use distance and the used layers so that'll return true if we are looking at a door that's on these layers that's within this max use distance away if hit.collider try get component door outdoor door if that returns true that means that the component that we hit does have a door component on it so then we'll get a reference to that door and we can check if door is open then we're going to close the door and if it's not open then we're going to open the door passing in the transform.position so this current transforms position so that'll actually let us open up the door whenever the player presses e but we also want to show that text to kind of give the the feeling that okay i can do this the hint that you can do this you're looking at something that can be done go ahead and do it so we'll do private void update we're going to actually copy paste these two conditions that we did on use because we want the exact same thing to be checked and i'm going to combine them into a single condition just make a little bit flatter and then say if door is open we're going to do use text dot set text passing in close and telling them to press e otherwise we're gonna do the opposite and say open we'll then set the use text game object to be active we'll set the position based on wherever we hit with hit dot point and i'm going to subtract out the hit up point minus camera.position normalized times 0.01 so that way we set the text just a little bit closer to the camera that would be on that weird kind of z fighting where the text is on the exact same z of the wall and you kind of get that flickering we're going to remove that by doing this offset of 0.01 in the direction of the camera we're then going to also do use text.transform.rotation equals quaternion.lookrotation passing in the hit.point minus the camera.position and normalizing that so that way it will be looking towards the player so it's oriented the right way if that first physics raycast fails or the hit thing does not have a door component then we're just going to go ahead and turn off the used text doing use text.gameobject.setactive to be false that's all we're going to do in here if we hop back to the unity editor so let's select this first door that says use to open slash close opens away from the player cool this already has a door it's a rotating door it's closed we're using all the default everything that's cool if i quickly just model another door with pro builder and we compare that to our current door you'll notice that it has the secondary kind of slice in the middle and if i rotate the door it rotates on a hinge if we compare that to this cube i just made and i rotate this cube it kind of spins more towards the center in pro builder there's an option to center the pivot if i click that you see the texture changes some and then if i start rotating it you'll see that it actually rotates in the center into that weird kind of offset thing that was happening before i'm just going to show you how to do in pro builder because again i don't know how to do it in 3d modeling program if you're using pro builder what you can do is choose this little edge selection tool select one of the edges that goes perpendicular to where you want it to go and press for me it's alt u and alt u is the same as insert edge loop in this pro builder window the important thing that that lets us do is select this edge then click set pivot do the object selection again and start rotating this object we'll see it now rotates on the hinge just like our door that's really important anytime you have a rotating object you need to make sure that's going to rotate from the way that you expect and with pro builder as you kind of expand and collapse your objects they don't always have the pivot you expect them to have so make sure you override it to be whatever you want it to be so the default settings we have make it work exactly like that where the door is going to open away from the player all the time if we use the used open close opens normally what i'm going to do is override the forward direction to be 1.5 because that's not something that dot products will ever return and so it will only ever open one way really you can choose any value of over one and it will behave this way or any value under negative one and it'll behave the same way it'll just control which way it's going to open by default the other thing we need to do is go ahead and hook up our player actions so remember the player actually needed use text i just made some 3d text that's disabled right click 3d object text and then resize it to be something reasonable it also needs a reference to the camera so i'm going to drag the main camera and configure the used layers to be usable i've added two layers here usable and player and i've not modified the physics colliding at all the doors are also on the usable layer let's go ahead and click play and see what happens cool as i walk up to this door we see this open e thing comes up if i move the mouse around we'll see that it updates pretty cool i press e wonderful it opens away from me let me open up the console okay so here you can see that the dot is negative 0.9 so i'm almost completely behind this door is what it thinks if i cross the other side we'll see that the door then opens the other way because the dot product is again a negative number and regardless of where i position myself around this door the door will always open away from me this makes a lot smoother gameplay for your users whenever they don't have a door hit them in the face if i go over to the use open close opens normally door we'll see the door opens away from me by default so we can't tell the difference until i come to the other side and then i try to use the door and we see that the door opens towards me interactable doors are cool but sometimes maybe you just want doors to automatically open so let's take a look at those as you can see we only have one door trigger per door that wants to automatically open we'll open up the door trigger class at a private serialize field door called door and on trigger enter that accepts the collider other we're going to do if other dot trigger component and i'm going to use the character controller here you can use any component that you know is on the same game object that has the rigid body or the character controller i'm going to check if the door is not open if it's not then we will open the door passing in the other transform.position and i'm actually going to copy paste this into ontriggerexit which also accepts the collider other because we're gonna do the exact same thing except check that if the door is open we will close it if we hop back to the unity editor drag our two doors to our two door triggers that we're using for the rotating doors and again we're going to change the door that automatically opens and closes normally this time we'll set the forward direction be negative 1.5 and one other thing here is we're going to go ahead and mark the doors as the default layer because we don't want to show to the player that they can use this door because it's going to automatically open for them there's no need for them to try to use it and mess up the automatic magic that's happening i'm also going to hook up the sliding doors door trigger door reference so we don't get a null reference exception let's go ahead and click play and see what happens there we go works perfectly the door still opens away from the player whenever we walk into the zone and we can of course customize the size of this trigger to be any size or any shape that we want it to be the one that opens like a normal door opens away from me but then i go to the other side and it hits me in the face so that's not ideal but is more realistic doors do like to hit me in the face now we gotta figure out how to do the sliding doors we're gonna follow the exact same principles we just talked about for the rotating doors of assigning the initial state on awake and then sliding the door whenever the player either uses it or comes within the range since the sliding doors don't really need to move away from the player we don't need to consider the player's position whenever we're doing the sliding opening because they're just going to slide off to the side the thing we do need to worry about is that the door frame is large enough to hide the door whenever it opens because if these doors slide left where we have the rotating doors you'd actually see the door come out the other side and that's not really a good experience right let's go ahead and open up the door script again and start implementing the sliding doors at the top we'll add a new header called sliding configs to group our new two sliding configs that we're going to create which are a private serialized vector three slide direction i'll set that to be vector three back by default a private float serialized as well that's a slight amount set to be 1.9 by default this is going to be how far the door should slide when it's opening i chose 1.9 because the doors are two meters wide so it'll move most of the ways you can still see it a little bit poking out and it won't have any z fighting with the edge of the door if i set it to be two exactly so we chose just a little bit smaller number than the door size we'll define a private vector 3 start position and on awake we'll assign that to be transform.position if we scroll down and take a look at the open function we have this condition to check if the door is a rotating door so we'll say else animation routine equals start crew routine do sliding open we'll define that do sliding open as a private eye numerator do sliding open we'll define a vector 3 and position to be a start position plus slight amount times the slide direction so that's going to take our starting position we're going to add in how far we want to go and then multiply by the direction we want to go the important thing then is that the slide direction needs to be a normalized vector if it has something larger than one will actually go farther than the slide amount so that's one thing to keep in mind when you're setting the slide direction we'll set the start position to be the transformed opposition we'll define a float time to be 0 and set is open to be true we'll then do while time is less than 1 and we'll do transform.position equals vector3 lerp passing the start position the end position and the time again yield to return to null and then assign time to be plus equals time the delta time times the speed very much the same as what we're doing on the open and close from before there should be no surprises here speaking of no surprises let's go ahead and do the close in close we'll again check if it's not a rotating door so the else condition here we'll do animation curve routine equal start curve routine do sliding close and at the very bottom we'll define that as a private i numerator do sliding close and you already know it's going to be in there we're going to do vector 3 and position equal start position vector 3 start position equals dot position we'll define a float time to be zero we'll set is open to be false and while time is less than one we'll assign transform position to be again vector3 start position and position time yield return null and assign time to be plus equal time delta time times speed if we go ahead and hop back to the unity editor we'll go ahead and select this first door uncheck is rotating door the slide direction being negative one on the z looks like it works out well because that's going to go to the right and we'll leave the other configs alone the other automatically opening door let's go and make it go the other way so it'll slide to the left by setting the z to be a positive one and we'll also uncheck the is rotating door for that one let's go ahead and click play beautiful i walk up to the door it shows me my text i go ahead and press e it slides off to the right just as we expected i can walk through and close it again the automatically opening door works the exact same way just going off to the left instead if we go ahead and update the speed to be something i don't know three three times as fast it's kind of got that more sci-fi feel where the doors are super fast and just open up you can even imagine like the air hissing as it goes by with that you should be able to implement doors into your game without any issue that can open a variety of different kinds of ways either being used automatically opening rotating or sliding that covers most of the cases that doors could be maybe you can come up with some other ones but this should give you a really good foundation to implement doors into your game if you got value out of this video please consider liking and subscribing to help the channel grow reach more people and add value to more people there's new videos posted every tuesday and i'll see you next week
Info
Channel: LlamAcademy
Views: 46,456
Rating: undefined out of 5
Keywords: Unity, Tutorial, How to, How to unity, unity how to, llamacademy, unity doors, doors in unity, how to make a door unity, how to make doors unity, rotate door away from player, sliding door unity, rotating door unity, rotating door, sliding door, make sliding door, make rotating door, make sliding door unity, make rotating door unity, make door unity, open door away from player, open door away, use door, door, doors, unity, unity door, door unity, doors unity
Id: cPltQK5LlGE
Channel Id: undefined
Length: 19min 35sec (1175 seconds)
Published: Tue Nov 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.