VR Character in Unreal Engine 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video we're going to finally cover how to implement a full vr character what i mean by that is using the cartoon classroom wheel engine and the karate movement component in vr when i attempted this in the past i started by using the pawn class and so i had to kind of implement everything from scratch that included collisions scaling the capsule and then attempted to obviously implement gravity and it just felt so much of reinventing the way that i really wanted to just use the character class i knew for a fact that it could be done as somebody from the discord server pointed out actually they did it and it worked and so just went back and really tried a little bit harder to implement it and i think well it worked and so we can cover how that you know functions i guess in in a real engine now i've already went ahead and created a project it's based on 4.26 and it's the vr template besides that i haven't done anything else that's just a mirror here we're not going to cover how that works it's just a render target with a c capture component and that will help us to look at ourselves why we're moving around and see what happens to the cops and to the character and that's pretty much it you can get full access to the project from this tutorial by going to buymecoffee.com for slash just to devs and click on extras you can support us by directing by this project or you could become a member and support us on a monthly basis by doing so you get fluxes to the entire library and become a buy me coffee member on our discord server so let's get started this video is gonna probably be a little bit longer because there's there is a lot to cover but you'll find timestamps in the description so that you can easily navigate to whatever part of the video you you just want to follow the first thing we do is obviously creating a carton blueprint so just create the prototype print i call it vr character and then we bring up the blueprint we just open it and what we want to do next is adding a camera to it so we're going to add a single component and we're going to call this vr origin we are going to call we're going to add the camera underneath that component and finally we're going to add a cube i'm going to scan this cube down by let's say zero point two five zero point five two point two five and we're gonna make sure that the owner um on an oc so we cannot see um we're gonna see the cube while we're moving around but we can see it in the mirror and then finally we also want to have absolute no collisions on this this is just a visual scene where our head is and we are going to grab the vr region and then lift it up so that it's more or less uh what we expect the head to be and with that done we basically got the initial setup finished now just want to quickly cover why we're doing this so the vr region and the camera being parented to it is just so that we can actually move the camera around through blueprints coding isn't it the camera itself cannot be moved so if you actually go in here and start to say like set for example word location nothing happens the camera is driven by the headset and so we need to actually kind of come up with it it's all tricks basically and this is one of them so the camera gets paired to the c component that we move that sim component and we get the ability to move the camera around with that done we can now drop this guy in here and then we can make sure that if we go to auto process it's actually set to play zero you know if we hit play this actually works and so we should actually see when the camera is obviously working and we can set we can see the cube over there but we can't see the capsule so it's gonna probably be a good thing if we start by um adding a throw up soon here what i'm gonna actually do is copy these from another project open so it's a little bit quicker but we just feed in um all the values we need which are all coming from the capsule of the word location the capsule height the capsule radius and the more rotation we then set this to zero point um i guess zero five and zero point one in fitness and now if we actually hit play we'd be able to see the actual capsule there and obviously if we get now into the headset and it starts to to work you can see that the capsule is over there uh but our head is basically outside of the capsule and so it doesn't move the current doesn't move the with the camera we are going also to go ahead and set the tracking origin to the eye level uh we don't want to set it to the full level we want to keep it at the eye level and with that now we basically got the initial setup we can see the capsule we could we got the camera moving around but obviously we got the problem that really nothing happens we're not moving exactly as we would expect so let's fix that first that's the first thing we want to go through and fix now to fix this what we are trying to do is saying we want the character to be where the camera is at least on the x and y for now which is basically this one here and this one here not on the set the set it's a different story um and so what happens is that as soon as we start the camera kind of text takes over the the headset takes over the camera and we move somewhere in the world space but this guy doesn't follow us so the logic we want to implement is well we get the offset between actual uh actually the current character location and the camera location so let's say that the camera moved here so we get the offset between these two from there to there from the camera to the actor and then we want to move the actor by that offset so we move it where the camera is but what happens is that the camera also get moved and then what we want to do is reset in the camera to be where it was before we moved the actual uh corrupter so it's again another trick and it's a bunch of tricks for you to get this to work and that's what we're trying to go for so let's create a vector and we're gonna call it camera offset and now we go in here and make it a vector and then what we want to do is calculate the offset so we get the camera location so we're going to say get word location and then we're going to say get after location and we are going to say that we want to do these minus these so we do the camera location minus the octave location and this is our offset and then we're gonna get the camera and set it to the offset so now we have the offset then we need to start to move our character around so we're going to say add after word offset um and actually well we can do this but this doesn't really work well so okay so this is how i went about for the first time but the problem with this is that we are going to move the capsule around but we're not going to get collisions out of this so you could do this uh but this is actually the wrong approach and so this will work you could basically move around and you know the cops will basically follow you and the art will follow you but then when you try to basically uh stick your head through walls or objects where there wouldn't be any collisions so this is something that i try but it doesn't work the much better solution for this is actually taking advantage of the character movement and so there is a function called move updated component and the delta is not this directly we can't do that we need to firstly break this and then we're going to split this track and we're going to feed in the x and y remember we don't want to take into account the z for now we don't want to move the actor where the camera is because we're going to crouch and then the atom will sink into the floor we just want to move the the actor on the x and y and then we're going to say get after rotation we want to keep the same after rotation we don't care about rotation really and we just move the actor so the the camera offset so the capsule is here we move away from the cab so the capsule now the actor moves where the camera is but now we need to move the camera back because otherwise the camera is going to be somewhere where it doesn't make sense because it's been offset and so we do other word offset to the camera and the word offset it's again a um is basically this offset but is the oppo it's in the opposite direction so what we do is that we negate the vector so we get the offset between the two we negate it so we go in the opposite direction and then we do the exactly the same thing here we break it so that we can get the x and y component and we now have the behavior we wanted and so if we now hit play we should see exactly what you'd expect so if we get in vr well you see that the actor is not following us and actually if i get something um like an object beside me so if i get like a cube for example here lift it up a bit let's make it make it like a little bit bigger and so i lift it up again place it there and now he play if i try to go it's a little bit far closer if i try to basically go against that guy well it's pushing me away away so i can't i can't really stick my head through through the cube and um that's basically this this is the basics of how we are going to do everything it's a matter of understanding where the camera is where the doctor is and then adjusting uh the actor and then after that the camera and the controller is once we have starting point the controllers but these are the basics and so now we can move on to uh i guess the next step and start to spawn the controllers so let's go going with the controllers and actually create a blueprint for the controllers and so we're going to call it like just bp controller it doesn't really matter and all we're going to have in here is the motion controller i'm gonna go in the construction script and then set the motion source we're going to create a hand variable so it's on the usual stuff the controller hand and we are going to set these to this and with that done i think that's pretty much what we need from this i don't think we need anything else and what we could do is also display the device modules that we can see and now what we can do here is uh go in um through spawning the two controls so we can say spawn after from plus we got another one down here there we go we're going to make a transform so it's just a normal spawn actor node and then we're going to say that i think i called it vp controller vp controller and i think i haven't exposed this so let's let's expose these to uh to spawn so we're gonna right click and refresh and actually i think i didn't compile that so i didn't get it refresh again and now we got the hand we're gonna say this is the right hand and we're gonna also make sure that it always spawns uh no matter what get this guy a little bit lower and then we can actually promote these two uh two variables we're gonna get the left and right right controller there we go and once i've done that i can actually snap these actually attach the actor to the component and we're gonna say that we want to snap let's not keep word for the for the scan so this is all the usual setup that you will see normally uh even the classifier pawn from the plate nothing new and so we got this now if we play this um we're not gonna get the controllers to be where we'd expect that to be i'm not sure they are not even tracking so far let's see what's going on there um so we are spawning them and attaching them to controller here and that's the left and right and i think they are let's say that there so they are not even tracking the reason why they're not tracking is that they need to actually get the owner here so i completely forgot that so without the cell phone and the the truck that won't work at all and now they work but they don't at the same time that offsets somewhere where you wouldn't expect them to be and they're kind of behaving weird there's a moment around they seem to as you get offset by how i'm moving around and that's actually a real problem so they're not where you'd expect them to be but i've also been offset by how i'm moving around my carotid which makes sense they are attached to the character but then i am moving the crowd around here so we kind of need to compensate for that so we can fix that first because we can go ahead and fix that first and actually offset the controller so if we bring these two guys in here we can say um add word add actor word offset which is the same thing we're doing for the for the output through the character movement components really we're gonna teleport them and then we're gonna get this and it's exactly basically the same setup so we're going to actually split this strat and get these two guys in there and there we go so now we got the controllers basically offsetting through and now if i go in vr it's taking a little bit of time to bring it up for some reason the rift keeps doing this well now they are like at least on the x and y they are where they're supposed to be so it seems fine i'm moving around and they keep staying where my hands are on the x and y they're definitely wrong on the set and so there's there's another yet another problem that solved i guess and so let's bring all this stuff lower and we don't need we do need basically a way to reset the controller's height and so what we need to do is actually let's implement the function let's set let's call it reset controller's height and we are going to actually get the left controller here and say set after location and then we're going to do the same thing for this one here copy that over plug it in and now we got it ready and so what we want to do here is get in the left controller and get the actor location let's split the struct so we want to basically keep the x and y uh the same for the uh for the actual location so we don't want to modify the x and we just want to modify the set and the set should really be the z relative location um to get the word relative get sorry the relative location let's split this then we're gonna get the get the after location and we are going to also split this and then we're going to add these two together and then add it here so we are using the actual actual location plus the relative location the current relative location of the vr region and now if i do that that brings them i should bring them up but well actually i haven't called that sorry that should be in there where we're there but we need to do that i guess in here and um actually we need to do that before this starts ticking before the camera starts to track and that's because we need it also for resetting dvr origin once we get there so let's actually implement that this now and it's going to become a little bit clearer later i guess but we are going to filter this will logic here before it starts with the gate so that it's close to start with and then we're going to add a multi gate here and i'm gonna add an extra pino2 and let's say on the last one we actually call reset controller height and after we do that we go ahead and actually start to track these um and this is just so to delay this a tiny bit it could be done different it doesn't really matter it could be like that the tick is disabled and that we have a delay at the beginning and then we do that but the thing is that now we have the controllers where they're supposed to be and if you look at this if they just move around normally i can move around and apple collide against that thing and the controllers are exactly the right height and so to recap what we've done is that we need to offset them continuously on the x and y because we're obviously moving the part around uh but then there's there's also a problem in the height and it's probably because we keeping these at the eye level and so what we end up doing is saying that the controllers should always be where they're on the xy but on the z is the up to location plus their relative location on the vr region and that gives us the right spot uh and the reason why we're not just doing this straight away we drop it into functions that we need to reset the control reside at some point later and so this is useful now this is where the really interesting part starts and it has to do with the capsule height one thing that we obviously want to achieve here is being able to move the camera up and down and scale the capsule together with the camera isn't it so the capsule should shrink or become higher place on the camera height um so the problem with it is that you can't simply go and say well let's get the camera location let's calculate an offset of sword isn't it then just scale the the capsule itself it wouldn't work and to give you like an example what that would look like is this if i just go here and say let's let's calculate an offset on the capsule and let's say that this capture capsule should be way lower isn't it so we want to basically make it smaller so we move this down here by a certain amount on the set and then we go to the capsule we just want to say well we want to match that amount well you can do that and it kind of works but now you're floating you there's basically some space below you that doesn't work so what will you do is that you basically start to fall and you try this and you start to bounce and also of uh collision issues so you can't really do that and after some thinking with these well i came up with a solution which is you do move initially the origin here by a certain amount of dvr region so let's say that sorry the camera by certain level let's just move the origin for now around let's say that it moves by 20 so it goes 20 lower than it is right now what you do is that you get the actual after and you move it down by 10 then you get the vr region and you move it up by 10 and then you actually get the capsule and shrink it by the same amount and by doing that you actually ended up exactly where it was supposed to be by however much you moved which in this case is 20 and so like from your point of view it doesn't look like you moved at all but with all these the capsule has shrinked classically to the right size and you are exactly where you're supposed to be on the floor and this happens in a single frame obviously so there's no um there's no probably collisions and we can actually turn apart the wolf in there and and that's literally how that works so let's actually go ahead and undo all these and see how that can be implemented so if we go back in here we need a bunch of variables now so let's move these out of the way um and besides what i just said the other problem that we have it's what happens when you go up and down stairs and so we need to include that too but let's let's go a bit and let's start by getting the um so lovely sorry but this one here the capsule component scaled capsule um hull fight yes and then promote these to a variable and we're gonna call this variable in it so um height and then that's the variable that we got available now then let's get the curtain movement component that uh let's get the current floor so we need to know where the current flow is and this is specifically for the um for the star scenario and then we want to promote these to the uh last floor so we now have a kind of last floor so last frame floor uh variable then we need two more variables and one is the actual get word location the last frame camera location and we've moved this on the various last camera location and then finally we need the vr region um to get relative location and we promote these to the vr or region location so this is the starting location of your region and this one specifically we need so that we can actually reset ddr region while we follow it so now let's get a new pin in here because we're gonna have like some extra logic let's get a branch and the first thing we want to check is are we actually falling and we can actually get the cutting movement component say i think it's falling yes it's falling and then we can say if he's not falling so it's the exact opposite of what you will expect if it's not falling what are we gonna do now if we are not falling um we want to get here a sequence and we want to get a branch and we need yet another bible checking if we were actually in the falling state previously so if we was falling and we're going to say if we are falling we want to update the current last floor to the new floor so it's not 5 00 sorry current yeah the current floor then we want to say that we are not fully anymore so we are updating the state and then um we do something else here i guess but yeah we're not doing that for now and now once we get in here where we have all the logic to actually understand what happens if we're not following how the capsule scale otherwise um we want to update the last camera location by just getting the camera location so we're gonna hover this over here and then we're gonna say if we were not falling so the opposite then we want to run some logic here so these wall these these two parts here are just to make sure that we when we switch between falling not falling we run this logic once so in here what we're doing is saying well if we're not falling well if we are falling sorry keep updating the last counter location because it's important we need to have that refreshed every frame regardless of whether we feel not and if we were not falling so um if we actually were the phone that we started falling but we want to reset the capsule height so this is where we have the logic for the capsule now if we go from falling to not falling anymore we want to make sure that the new floor that we touch becomes the last floor because this is this last blue variable is useful when we go up and down like slopes or stars and we want to make sure that that's not the case anymore and so in here you will see that this will become like this so now we are for it and and then in here we have the uh running logic on why we're touching the flow which needs to run continuously so let's start to implement that the first thing we do is get in the camera location and i'm going to say that minus the last camera location is the offset basically of the camera from the previous frame to this frame and we could create a new variable we don't need to we already use this here and this is a sequence so we can kind of use the same camera and it's it is a camera offset in the end um it wasn't really camera offset here so that's a little bit confusing it's it's more like camera to octo offset isn't it but we're going to use the same variable so we can get that in there and now we kind of need to run a similar logic to what we run up here in which we are moving the curve that our character around that we reset we move in the opposite direction the camera and then in this case we set the capsule height but it's all based on the z so now we're working with a z not anymore with the um with the x and y so we're gonna say add actor word offset and we are going to actually split this then we're going to get dvr region and we just said got outward offset same thing let's split this track because we won't work just with the set i'm gonna get the capsule and say set capsule half height and we call update overlaps it doesn't really matter and then finally i want to get the uh left and right control action i'm gonna place this over because anyway it's the same thing as the edge wonder so we get the left and right control and we want to update also the z location and now we need to calculate this location and we're going to do so by creating a get camera set offset we're gonna make it pure and then we're gonna go here in the float i'm gonna say this is the reset and depending on whether or not we are going up or down stairs or like a slope we want to rent on a different value so now if we need to check first if that's the case so what we're going to do is that we're going to get the um capsule sorry the current movement corner floor so we're going to say get current floor and then we're going gonna get the other current floor um and then we're going to actually break these grab these and get these locations we're gonna actually uh do this minus another vector and we can nicely pack this into like a function so we can say that this is like a gap to our location function because we need to use it also for the other one here for the elastic floor so what we're trying to say is is the current floor and the last floor here um at the different location that's what we're really trying to get at then we split this and we say if this is not equal to zero that is the case at that point so we are in fact on a on a different level and then we get get the camera offset split these and say if that is not the case we just return that offset so if we are on the same level if we are walking always on the second floor we don't care it's just the normal offset if that isn't the case though we want to actually do the set of this offset minus however much we went up or down because we want to calculate the offset and this particular logic kind of removes um well issues with the controllers first of all but also like bumpiness uh in the characters movement and so now if we go back and we get the camera offset here we can use it in several places now we divide this by two so remember we're talking about how we just move the actor down by two then we go in the opposite direction so we can do these times minus one here and so that's now going in the opposite direction so we just specifically to the actor down and then we lift up by half isn't again the actual they are the original so the camera is now where it's supposed to be but now we need to basically scale the capsule and so we get the capsular fight and then we add on top of it so we get the current uh value and we add on top of it the uh this value here so half again that value and with this done we basically do have the entire process if we don't take care of the controllers though there's going to be problems the control world will actually as we go up and down that will actually get offset in very much in the same way when we're moving left and right isn't it that we're getting offset on the x and y and so we need to run the exactly the same logic as the vr region because they do go together basically the offset is kind of um related to each other and now we can we can we could run this but it wouldn't work we still need finally to update the last camera location so we need to ensure every frame once we've done all these calculations we are basing all these on the last camera location which we use in here we need to update it but we also need to update the last floor location and so we want to make sure that that that is exactly updated every every frame at least in this in this case we don't do it in here but you remember when we come back from these we updated here once um and that is pretty much it for this so we can already test uh but we still don't have the logic for when we fall in so we need to cover that and so now if i if i go in here you see that as hazel moving up and down this is actually scaling with me and we're moving around and it's actually scanning together with me and i can move around um now this is um this is great but it still wouldn't work when we fall in and i can i can actually take ourselves on top of the stairs and i can show you we haven't implemented inputs yet which maybe you should have done by by now i take this guy up here i'm gonna lift it up and sit on top of the stairs should be able to actually now get in there run and 45 yeah just fell and as you can see there's some issues there the capsule is like really high it just fell and the capsule is like all the way up there because obviously there's not logic that fixes while we fall in and we need we need to always implement that and so why when we start to fall we kind of want to get into back into the initial state we want to reset everything isn't him we want to make sure that we reset the region height so when the region is positioned completely how i want to make sure that we reset the capsule to make sure that we reset the the controller side and we're not doing any of that so we do need to actually get uh add again some afterward location so we're trying to basically run all the same logic but this time we're really trying to just um set the capsule height to be the region height and so we do run a very similar logic and so what we do is actually this so we do get the capsule um half height we do minus the initial height so we get that offset and then we um we don't we don't need to divide these actually i don't know why i do have that in there i think i was messing around with some values um we decided that to be the world offset that only on the out and then we get the capsule height we passed in here and we want to basically set that to be just the initial height um so that just gets resulted with initial height and now we need to reset the controllers and if we do that we want to reset also the dvr region so this idr origin it's a new method that we need to write and in here we just want to set add some word offset to the current um recurrent thing and what we're doing here is running this logic which is actually pretty straightforward we get the current actor location and we know that the actual location needs to be added by the original relative location on this set and then once we do that minus the camera location we get the current offset by which tv origin should be lifted to get back to where it was originally set and if you remember if we go here to begin play the region here the original location is the initial priority location so that we go back exactly to where we were originally and that's basically the offset that we need to type in there now if we drop this this one here with actually that up here and then we make sure we run these and it doesn't really matter but yeah i guess it's just for peace of mind we do have uh the whole logiter to actually reset why we were falling so if i'm going here now and i'm going there i think that go into a little bit of a mess let's see if i can get on top of the thing um i think we should really implement just movements so that we can see this this is actually basically being shipped all the way up for some reason yeah but they are more or less where it should be even though falling from really high for some reason so maybe i should go all the way back here let's see where i am yeah i'm standing up here and let's see if we can actually get yeah and as you can see basically um i felt right here and when i when i basically touched the ground i was at the same relative location compared to the capsule um so that works perfectly and now we um we also may want to actually do that reset here at the beginning so we want to make sure that we reset the orange at the beginning just so that we can keep that original z offset even if we let's say scale up the um the capsule to be different height and fdv oranges so if we made these really high like 116 or something like that and then we lift this guy up that's exactly what we got when we start so if i now get this guy yeah i'm exactly where i'm supposed to be some really tall what is it way toward that that you'd expect and you got the control is here at the right height as you can see i can't touch the ground and that's just because if that's just because of these if i will run that logic i will be at the right height there so we can reset that and get the origin back there and that's basically the height implemented this is basically almost the end the last thing that we're missing is being able to move around using the uh motion controllers and we got everything set up we just need to use the character movement component to move our current around as you would normally do even 2d game so let's uh let's go to actually the project settings and make sure that we go everything we need we go to the input and i think we already have this setup so actually you can see we got the moisture controller found left x and y um and that's exactly what we needed so we're gonna bring it up here so we're gonna say motion controller um left underscore um y that's one and then we're gonna do this again with x and that's the other one and then we're gonna move these two um like this and we are going to call add input vector and movement but not the impulse vector yes that's only character movement component as as i was saying that's what we're using to move around um and we can basically got it above here and now what we want to do is move it in the direction the camera is looking at so we can get the camera word rotation i guess and then we can get the um we can break this i hate it actually it does not always get the wrong one uh and then finally we can make a new uh rotator and just to explain what's going on i am trying to get the forward axis of the camera but on a 2d plane and so by doing this i just copied this regarding the rolling pitch of the camera and just look at the u and then we can say get a forward vector and we can multiply this by a float and that float will be the axis value so the less we press the um the thumb stick the slower we're gonna go as you would expect from a normal uh 2d character and then we do the same thing for the for the x here uh actually sorry it's not bad you get the right vector in this case because you want to move obviously um on the right direction isn't it and with that done i should be able to just go in here run the headset which this time come on very fast which is great and then just move the thumbstick around and as you can see i'm just moving and i'm getting full collisions as you would expect i can push against things i can go upstairs which always keeps the perfect offset and they can jump down the head is exactly what you expect it to be like i just move around and and it behaves exactly the way you'd expect it to behave and now the cool thing about this is that well because we're using the character movement component well all the nice implementation of these comes into play so you can you can decide the max step height you can decide all sorts of things you can decide to have very low gravity and you work exactly as as you'd expect so you go up the stairs and when you fall well there's low gravity so you're just kind of floating um you can increment your speed you can do all the things that you would do um normally i guess so say my normally uh depending on some of these parameters you know with the current movement component and i guess that goes also for replication and that's basically a full vr character thank you so much for watching until the very end i really hope you enjoyed the video obviously i just want to say that this is an implementation of adr character this is not the implementation there's actually lots of flaws in the implementation of the experience for example sometimes hitting play because the camera is not controlled by the headset within a certain time that you jump really high uh so there's all sorts of flows with it with the current implementation this isn't like the final fledge you know production ready uh vr character that you can just throw in your game but it is a good start for you to learn how this could be put together how this could be taken forward hopefully or learn some basics um around like how their character would work and a ton of fun putting together and i really hope you enjoy the content and if you did leave a like so that we know that you like this format you like content that goes so much in depth and it explains things if you didn't leave a dislike so that we know that you don't like this kind of content and you don't like this kind of format besides that don't forget that we have a discord server where we can toggle things vr related whether it's like games your development tutorials the vr content examples go and join is free you you can just join and i guess we becoming more and more and it's just a nice place to discuss your content and i guess i'll see you next time bye
Info
Channel: Just2Devs
Views: 797
Rating: undefined out of 5
Keywords: Unreal, Gaming, Programming, ue4, unrealengine, VR, virtualreality, VR Character, ue4 character, character, virtual reality character, ue4 vr character
Id: pX-j4HQNAiY
Channel Id: undefined
Length: 42min 14sec (2534 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.