UE4 How to - Sliding Door using Physics Constraints in Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I'm Andre I'm gonna show you how to make this physics based sliding door so let's see how it works so we can open and close it using a certain key and if we put objects in a test it's gonna push those objects out of the way and as we'll see here it will also stop it the objects block block it and if we remove the object then it continues closing so let's see how we can implement this so we're going to use the physics constraint that it's gonna limit the door movement on the horizontal and it's gonna make it want to come to two locations one which is it's going to be here for the open and one is going to be here for the closed so let's create an actor I'll use two cubes for the walls like this and we'll duplicate it for the other one like that and we'll use another cube for the door that we're gonna call door like this now let's move this one out of the way so it doesn't collide with the door okay so now we've got we want the door to be sliding on a horizontal direction but to stop somewhere over here and over there so we'll use the physics constraint for that okay and here we'll leave the first component empty which will make it connected to the physics the the actor itself and the second will point the door like that so in order to limit the movement we'll limit everything and just unlock the movement on the x axis and we'll put this to limited and 250 units so this will leave it will permit it to move 150 units so one meter and a half to the left and what meter and I have to the right and because this has three meters because the cube has one meter and with put size of three here then this one is three meters wide so it's gonna be moving from this this extremity will be from here to here I'm sorry from here to here okay so let's try this out just drag this in or rotate it and we'll just drag it up a bit and yeah we forgot so the door has to simulate physics like that so let's try this now so as you can see normally when it reaches the limit it stops dead but here because it's colliding with this it's bouncing back so if you don't want it to burn bounce back just pull this further away okay so now we wanted to stay in a certain location using the force so for that we're gonna use and physics constraint here we're gonna use the target linear motor and a position target and a velocity target so we'll activate these on the x-axis the position target we'll use a force to keep it in a certain location and we'll beat this force to 200 and the velocity target will use a force to limit its velocity so this will put 220 let's see so the velocity target we'll just using so we don't so it doesn't bounce it doesn't wobble when we move it so if you try again now I pull this as you can see it comes back now what we want to define is the the position where it wants to stay so one will be here and the other one will be there so one will be for open the other one for closed so in order to do that to command it using the certain key I've implemented an interface call in the player so when we here what we're gonna do is go to the class settings and we're gonna implement that interface is called use i underscore use so when we implement it it gives us an event use like this that is going to be called each time we look at the door and we press the e key so here we're gonna make it open and close so for that we need a variable that we're going to put here called closed this variable will remember the state of the door so by default will be it will be open because this is false so this is gonna dictate the values of the location of the door so we'll put one and four closed here somewhere at one hundred and fifty and one position here - hundred and fifty so let's see so this we're gonna select this a float so if closed is true then it's gonna pick a so here for the closed I'm sorry about that for close it's 150 let's just actually we'll put this back okay let's add the Select again so here it's gonna be hundred and fifty and here - hundred fifty so how do we tell it to stay in one location where we're going to change this the linear motor position target so we'll put I'll take the physics constraint and we'll set linear position target like this so we'll plug this into the X so now when we push e it should work but it doesn't and the problem is that the door in in the meantime the door is actually going to sleep so we're gonna have to wake it so when it's going to sleep it's not going to update its physical position so we'll go to call wake rigid body on the door so if you try again now it should work so push e again yeah so it doesn't go back and that is because I actually haven't changed this value so once we close it I remember by default it's open so once we close it we want to negate this and write it back here so we're gonna set the closed like this to closed negated like this so now we should see it change States so as you can see it opens and closes like it should now if we so as you can see it works now here what we want to do now is we want to set it because if you look here it's said to be somewhere in between that's because the face is constrained when we set the limits here it only lets us set a limit symmetrically left and right from its position so we have to actually leave it here but when we start the game we want it to be either closed or open so in order to do that we're gonna have to set its minimum motor position target and set its location its relative location so because the door is not parented to anything but it's parented to the reactor bill doesn't matter the thing is we cannot set the its relative position because it will take it as world position because it doesn't have a parent component so we're gonna have to set it by the world occasion and convert the local to world so we'll set here set world location like that and here we'll get the actor transform and we'll say transform location so this is actor to world so here we're gonna have the just get rid of these we're gonna have the relative location and it's going to be transformed into world so we're going to set the world location now here the relative location it's gonna depend also on the clothes the value okay so let's take it like this and actually split this here and we'll plug in to the x-axis so now when this is closed when the default value is fall so it will take minus hundred and fifty and we'll set that location so let's see this so what we saw actually is that it jumps here now that's good but we also wanted to want to set the position target to that one so take actually this one like that and normally we should have the same thing so we'll take it from here because if this is the local local coordinate these are these are world so we need local for that and this will plug in the physics constraint like that so if you do this again well we see that we have a problem actually so when we start it actually goes here and instead of going the other way and that is because I don't know exactly why but the physics constraint actually has its axis flipped so instead of the x-axis going that way the positive it goes that way so what we have to do is here we're gonna have to flip those so we'll multiply with a float which is gonna be a minus 1 like that so plug this in so we're actually changing instead of putting a hundred and fifty will put a minus 150 and that is because the axis of the physics constraint are flipped so if you try again as you can see it teleports here and if we close it it works as it should and if we put an object in its path like that as you can see it works we can remove it we can pull it open and it works so that's it if you live like this please like share and subscribe and I'll see you in the next video
Info
Channel: Lusiogenic
Views: 4,656
Rating: undefined out of 5
Keywords: tutorial, unreal engine, gamedev, ue4
Id: W1dv_OKqhD4
Channel Id: undefined
Length: 14min 10sec (850 seconds)
Published: Wed Sep 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.