Introduction to VR in Unity - PART 4 : CONTINUOUS MOVEMENT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back to the part 4 of this tutorial series that will teach you the basics of VR development in unity in last episode we learn how to move in VI using teleportation and into this video I will show you how to set up the other most popular locomotion system in VR to continuous movement I'm Val am and this channel is all about VR developments so if you don't want to miss the next video make sure to subscribe and ring the notification bell but without further ado let's jump right in the tutorial ok so we are back where we were at the end of last episode by the way if this is the first episode that you've watching don't worry you won't need to implement everything from the others episode all you have to do is ever mix our rig setup like this which you will find out in the first episode of this tutorial series now as you can see I took the time to add a bunch of obstacle by moving and scaling some cubes over here that I will use to test them touchpad movement that we will implement ok so let's get to work first I will select my VR rig now in this game object I will create a new component that I will call continuous movement now let's double click on this script to open it the first thing we need to do is get the touchpad input for the player I will start by writing at the top using unity and line that X R and using unity and shine that X r dot interaction dot toolkit this way we will be able to access the element in these namespace now I will create a new public variable of type X our node that I will call input source now if we save our script and that we go back to unity you can see that with the X our node we can select a particular input source so in our case we want to listen to the left hand now back to our script let's create another variable private this time and of type vector 2 called input axis now as we saw in the part 2 of this tutorial theory we can listen to an input by accessing the device and we can access a device using the characteristic of it with input devices that get device with care activist tics but another way to access a device is using the EXR node which is maybe a little bit more straightforward than the other way so let's do input device without s device equals input devices this time with nests that get device @xr node with our input source and then go know that we have our device we can listen to the input with device that try get features value and in our case we want to listen to the touchpad for the movement so command usage that primary to the axis and we can set the output to be our input axis viable perfect okay so now that we have the input let's move our rig to do this I will create a new private variable of type character controller cool character so this character controller will manage all we can move the rig when colliding with an object like a stair or a slope for example and you will see how in a minute now for the character controller we can access it at the start of the game with gate component of type character controller for the actual movement of the character we won't do it inside the update function but inside the fixed update function instead this way the movement will be computed each time that unity update the physics of our game now to move based on the input we can create a new vector of equal direction on the x-axis the direction will be input access that X 0 on the y axis and input axis that Y on the z axis finally we can move our character using the direction with character that move Direction important here as we are moving inside a fixed update don't forget to multiply by time that fix Delta time also if we want to better control the speed of our character we can create a new public float variable called speed that I will set initially to 1 and multiply our movement by this value perfect now let's save our script and go back to unity before testing our game we need to add the character controller component that we are using in our script so I will click on add component and serve for character controller as you can see this component as different settings for the slope and the step of the movement that you can use to tweak our player will interact with the obstacle but in my case I will just reduce the radius of our character controller to 0.15 and set D hunter to zero one zero so that the capsule Collider will start at the beginning of the rig perfect ok so if I press on play and you can see that that I'm able to move using my left joystick awesome and if we go in the inspector and that we increase the speed you can see that I'm able to move faster but there is a little issue right now we are always moving in the same direction and not the direction or at setting so let's fix this back to our script to set the direction towards where the player is looking we need to rotate our direction by the edge yaow so first to access the players add I will create a reference to the excel rig which is on the same game object by creating a new private variable of type X our record rig we can access it at the start of the game using a gate component of type X our a key and now using the exotic you can see that we can have access to the head game object by calling Ric that camera game object and in our case we can get the EDL with quaternion at y equals quaternion that Euler 0 on the X for the y axis it's our rig dot camera game object the transform that Euler angle dot Y and 0 on the z axis and now we can rotate our direction by multiplying the direction vector 3 by our EDL rotation perfect now let's go back to unity to test our game and there you have it you can see that already we can move and the movement will always be facing the direction we are looking at awesome but for now we have only move our character under originally and not vertically using gravity so as you can see if we go above this slope and that we try to jump you can see that we are not falling so let's go fix this back to our script to set up gravity we'll need two more variables a public float variable called gravity which I will set initially to minus 9.81 as a lot of you already knows this is the approximation of the gravity around the earth now for our second variable this one will be private and it will be of type float called falling speed so to make a player fall we could simply go in the fixed update function take our character and move it towards the ground in the same way as we did for the horizontal movement using character dot move vector three dot up x full velocity multiplied by time that fixed Delta time and maybe set the for velocity to minus ten beforehand but this won't be really physically accurate and you will see why in a minute now if we save and go back to unity we can see that if we try to fall indeed it's working we are going down but it feels a bit weird the reason is that the velocity of an object going down is not constant like this but needs to be built up over time so let me show you how to do it in our case now instead of the thing the for velocity to a constant value like minus 10 we need to accelerate it using our gravity parameter so by doing full velocity plus equals gravity multiplied by time that fix Delta time but this acceleration means to occurs only when we are falling and not when on the ground so let me create a new function called check if grounded which will return a boolean value as Adam suggests this function will be responsible to tells us if we are on the ground or not and indeed fix update function we can nubu varible is grounded said with check if grounded and then if we are on the ground we can reset the Felicity to zero otherwise we can increment it perfect now here we go what's left for us to do is to complete the check if granted function okay let's get to work now there are multiple ways to check if we are on the ground we could use a Collider and check if the ground is colliding with it but for me the best method is to use a sphere cast which is basically the same thing as a ray cast but with a certain thickness and we want a certain thickness because if we shoot only one ray at the center of the capsule and that we are on the edge of a platform the array can tell us that we are falling even if we are not so to do it let's first set the start of array in my case I will set it at the center of the character controller by doing Victor 3 ray start equals transform the transform point character dot center here the transform point giveth the character Center in world space now for the length of our sphere cast as we are shooting from the center of the character we need it to be at least character dot center dot Y but to be more safe I will add a little value let's say 0.01 so that the way will be just a bit longer than the character capsule and there we go now we can cast off fear cos which will return true if we eat something or false if we are not eating something I'm going to store this in a boolean call as hit and we can do sphere cast array start character dot radius so if for the thickness of the sphere case we want it to be the same as our character radius vector 3 that don't so this is for the direction of the Ray out ray cast it it info and finally our rail length now as you can see we can add a last parameter which is a layer mask that we can use to tell what we want to set as a ground and what not but I think this is important if we want to do things right so I will write ground layer and create a variable with this name on the top of my script set it to public and make it of type layer mask and there we go now what's left for us to do in the check if grounded script is to return P has hit file and now our script is ready we are able to tell if we are falling or not now let's save and go back to unity okay back to unity as you can see by default the layer mask is set to nothing we can change it to everything so that we will check the ground with every game object in our scene but if you want to be more precise what I suggest to do is to create a new layer called ground set all the object women or player to collide with to this layer so in my case it will be the big plane or the platform over there and now we can set the layer mask in a script to only the ground layer perfect now let's click on play to test our game and there you go we have now a working continuous movement that works great with a nice and realistic fall but we are not down yet because we have still a little issue with our character controller as you can see the character move when we move with the touch pad but when we physically move around in VR without the touch pad the character doesn't follow us this can make us not collide with objects that are in front of us so let me show you how we can fix this so back to our script I will create a new function called capsule follow at set now to make the capsule follow exactly the yet set we can first set the ID of the capsule to be the ID of the ad set in the rig which we can do with character that ID equals Ric that camera in rig space right now by doing so we will make the ID stop exactly at the place of our eyes so maybe it will be better if we increase this sight value with a custom value that we can create as a public float viable with the name additional height set to something small like 20 centimeters and we can use this value to create to increase the character right now for the center of the capsule I will do vector 3 capsule Center equals transform that inverse transform point Ric that camera game object the transform that position know what this inverse transform point does is give the local position the camera will have if it was a child of this game object so actually this will place the center of the capsule exactly on the head which is what we want horizontally but not vertically so finally we can set the capsule Center to be a new vector3 with capsule Center dot X on the x-axis capsule center dot Z on the Z value but on the Y value we want it to be at off of the character right so here we go like this now to reduce the risk of a bug while moving the character control uses skin wise parameter set to something really small which we can see in the character control component right there it's like an aunty arrow zone so if we want to take it into account we can increase the center to character that skin wilds and there you go all caps will follow at set function isn't already what's left for us to do is call it in the fixed update function before updating the movement of our character perfect now let's save and test what we made and there you go you can see that the capsule is correctly updating its position when I move around the rig and and when I go up and down this is actually really interesting because now as you can see I cannot go through an image geometry by moving forward and even better if I want to go under this bridge I need to crouch in real life to get to the other side pretty cool now if you go we got ourselves a nice touchpad move on locomotion system for VR now an important remark here if some of you feels like this movement is a bit jittery it's because the physics time step is too long so to fix this we can go to edit project settings time and you know you can see the fixed time steps used by unity to compute the physics in our game so in my case I'm using an oculus which has 90 image per second so I will at least need three time step to be 1 divided by 90 so approximately 0.01 1 1 1 1 perfect this way our movement will be really smooth now last but not least we can move so what's the first do is to implement a way to rotate using the other joystick unfortunately I got a really good news for you it's already implemented inside the XR toolkit so let me show you now in the etheric we simply need to add to our rig the snap turn provider component as you can see this component has a bunch of pairs the first one that we will be interested in is the turn input source so in my case let's set it to primary touchpad next we can set the controller that we want to trigger the rotation but in my case I will just add one there we can select one of the controller that we used for the direct interaction or the array that we made in the previous episode but in my case I would just set it to the right end controller finally below we can change the rotation amount personally I like 45 degrees but you can set 90 degrees maybe but what I'm actually going to do is reduce the activation time out to 0.2 this way we will be able to rotate more quickly and not wait 0.5 seconds each time and there you go everything is not ready now let's click on play and now we have a touchpad movement that works with snap turn awesome by the way I didn't talked about it previously but this system works on top of our teleportation system as you can see I'm still able to teleport while moving which is pretty good anyway that is all for today thank you for staying with me through this tutorial in next episode we will learn how we can better interact with an object to grab it and use it as always if you think this video deserves it you can leave a like below you'll be shout out to all of my supporters from patreon which made this video possible and welcome to the new one that joined this week and are on the right side of the screen right now and if like them you want to have access to the source code of all of my videos plus exclusive content and also support the channel join us the link is in the description below thank you for watching till the end and see you in the next video [Music]
Info
Channel: Valem
Views: 63,262
Rating: undefined out of 5
Keywords: Virtual Reality, Unity, VR, Valem, oculus tutorial unity, vr tutorial unity, Make your own game in Unity, vr tutorial beginner, introduction to vr development, VR basics tutorial, how to make a vr game in unity, XR tutorial unity, beginner xr tutorial, basics XR development, unity 2020 vr tutorial, vr locomotion tutorial unity, touchpad movement unity vr, continuous movement unity, player controller vr unity, character controller vr, xr unity tutorial
Id: 5NRTT8Tbmoc
Channel Id: undefined
Length: 17min 51sec (1071 seconds)
Published: Tue May 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.