How To Make A VR Game in Unity - Complete BEGINNER Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's create a new project I'm going to use the newest LTS version of unity which is the 20223 version I'm going to create it in urp and I'm going to name it my VR project once uh Unity loaded just go to window package manager Unity registry and we need to install the XR plugin management so just hit install and we also need to install the XR interaction tool kit so click install as well to also install the new input system so we need to restart the unity editor so let's just hit yes once the editor restarted we can close it and navigate to edit project settings XR plug-in management and we need to install the open XR plugin it's installed we see some issues first go to open XA and we need need to add interaction profiles we can do that with uh the plus button and I'm going to add the Oculus touch controllers uh The Meta Quest Pro and the W index so back to the project validation we only see one issues left so just click fix once it's done we need to set up a simple scene to do that we are going to use the cint studios polygon starter pack the link is going to be in the description so free to use package so just add to my assets and and import the assets to do that go to window package manager uh click my assets and search for polygon starter polygon starter pack and just hit import and import the package yeah we can close it now so just go to its folder prefabs and just drag and drop and anything uh you can see it's using the built-in pipeline so we have to upgrade its materials to the universal R the pipeline to do that go to window rendering and render pipeline converter and just take these and hit initialize converters sa and continue when it's done you can see the materials it's going to upgrade so just hit convert assets yeah and it's working now okay we can delete this one now and I'm just going to need this View and we are going to need to use this building flow 5x five so just drop it in uh reset its transform and scale it a little on this head and the y- axis okay this one also has a simple sky doome so just drop it in and also reset it transform and we also have a cloud ring also reset just the positions z0 Z okay we cannot see them so we just need to make the Dome scale a little bigger so try so try five yeah now we can see it next up to make it organized just create a empty game object reset its transform and call it environment yeah and drag these into it so next up we need to make an XR rig so to do that in the hierarchy just right click XR XR origin first off we need to set the tracking origin mode we want it to be tracked from the floow so so select the floor option and when we open it up we can see there's a main camera so we have to delete our other main camera from the scene there can be only one camera in the scene let's do that and as you can see there's a left hand and right hand controller attached to the XR origin we want to set it up by ourselves so just delete them so to set this up right click on the camera offset create empty reset it's transform it if it's needed and rename it to and rename it to left hand controller and duplicate it and rename this one to right hand controller Ty both of them and add an XR controller action based script to it so we could set this up one by one with the new input system but Unity made our life much easier it's already created a sample one for us the XR interaction tool kit so to import it go to window package manager change this back to Unity registry and at the XR interaction to Kit you can find the samples the starter asset samples and import it once it's imported let's go back to the left hand controller and at the the top right corner there's an icon to select the preset and for the left hand controller select the xri default left hand controller and as you can see it's already set this up for us so go to the right hand controller and do the same okay there's one last thing to do go to the XR Orion and at the input action manager we need to add an input action asset just select the xri default input inut action also we forgot to reset the XR Origins transform so it's all the way there so just do that and now we can start the game and look around in in the word yeah it looks like it's working we can look around but we can't see any hands right now so let's set that up from the Oculus integration package you don't have to do it you can download it in the link is in the description so let's import it go back to S and import the VR hands package and click import so to use it just go go to the folder and select the left hand controller and to the model prefab just drag the left hand prefab do the same for the right hand controller just drag the right hand prefab to the model prefab and that's it just start the game and and let's see what happens yeah as you can see I can see my hands and move it but there's no animation yet yet so let's set it up back to Unity if we want to make our hands animate we're going to need an animator controller it's basically controls which animation to play we can Define it by our C script so let's create one so at the VR hands folder just right click create uh animation controller and call it left hand animation controller on our prefab we have a animator component and we need to drag the animator controller to the controller field yeah then double click it and it's going to open up the animator window here as you can see there's a parameter Tab and what it's basically doing we can specify a float an integer a bull and a trigger and with this for variable we can we can control which animation to play so we're going to need two float values one for the grip value and one for the trigger value so let's create them call it grip create another one and we going to call it trigger in this case we are going to use a blend tree and with blend trees we can smoothly interpolate animation based on our parameters so to do that just right click create a state and from new blend tree so double click it and right now it's a one-dimensional blend type but we got a grip and a trigger so it's going to be a two-dimensional one we are going to use use the 2D free form Cartesian and as you can see at the parameters there's two grip right now so we need to set one of them to trigger and at the Motions tab here we're going to put our animations what we want to blend between so we are going to need four we just add four at the plus icon and add motion field so grip and Trigger values are going to be between zero and one so so at the position X and Y we have to set it between 0 and one so first we are going to use the idle animation when we don't have any movement at z0 position so we just need to open this left hand default animation fpx and and drag the left hand idle animation to the first one in the second one we want to do a pinch animation and I'm going to set the Y position to one and leave the X position at zero so at that position we are going to pinch so just drag the animation to that field and the third when we want want to grip we are going to set the x position to one and leave the y position at zero and also drag the grip animation to the field at the last one we have to decide what happens when we using the grip button and also the trigger Button as well I think the best thing to do in in this case is just use the grip animation because that feels more natural so let's drag the grip animation to the fourth one as well and the position X and Y remember in this case we are going to have our grip and Trigger parameters at one so just set the position X and Y that's why we have to set the position X and Y to one okay so at the trigger just Define it zero and as you can see uh we are at the idle animation right now so if you just modify the grip value you can see the Red Dot is moving towards the position X1 and also if you move the trigger it moves along the Y AIS and if you look at the blend tree if I move the grip you can see the which animation is started to Glow a little in this case it's the left hand grip if I do the trigger then ni slowly interpolates to the left hand pitch animation so we are going to use this in our C script based on our input so to do that we need to find out which actions we can use in our input action asset so to find the input action asset we just go to the assets samples XR interaction toolkit the version you have uh starter assets and you can find that the XR default input actions so if we double click it and open it we can see the control schemes and at the action Maps we just need to select the xri left hand interaction cuz we are going to interact and there we have the select and activate action the select is basically the grip on our controller and the activate is the trigger as you can see at the action type it's a button which is only able to return a Boolean if it's pressed or not as you can see there's a select value and activate value as well which is a value type and it's going to return a float value so we are going to use these two actions so you can close this now go back to the scene and in the assets folder go back to the VR hands and select the left hand prefab so we are going to add the C script to it call it call it hand animation new script and create an ad double click it and if we'll open it we are going to use the new input system so type using Unity engin do input system and delete the start function we are not going to use this one now we need to read these values from our controllers so to do that we are need a reference for the input actions we could use a public variable but I like to use serialized f private variables because then we don't have access to these variables from other script and I just like to keep these variables less accessible so to do that I'm going to use a serialized field private and input action reference and I'm going to call this one uh grip reference in the update function I'm going to read the float value so to do that just uh type reference. action. read value and it's going to be a flow and now it's a good practice to store it in a separate variable so to do that just type float and I'm going to call it grip value and now we have access to this value so to test this the easiest way is to write these values to the console to do that we we need to use the debug log and I'm going to insert the grip value here if you go back to Unity and click on the left hand control controller you can see at the grip reference we don't have any input action reference so we just need to select the xri leftand interaction select value just remember to use the select values so it will read the float value instead of Boolean select that and enter play mode as you can see if I press the button it changes values between zero and one so if we go back to our script the only thing that left us to do is to connect the animator compon component to the script and and set the animator's grip parameter to the grip value so to do that we can delete the debug. log and we are going to need a animator reference to do that again serial ice field private animator and call it hand animator okay if we go back to Unity and select the left hand prefab we have to drag the animator component to the hand animator field go back to the script and here we are going to set the grip parameter to the grip value just type in hand animator do set float and in this method we need a string name which is going to be the grip and the float value which is going to be the grip value just bear in mind because it's a string we need to have the exact name of the parameter so the the best practice is just copy it from the animators parameters step so I'm just going to copy the grip go back to the script going back to the script and I'm just going to paste it and comma we are going to insert the float value which is the grip value so go back to Unity and hit play so as you can see it's working perfectly so the next thing is we have to set up the pinch animation to do that go back to the script and we are going to do do basically the same thing so I'm going to need another input action reference and I'm going to call this trigger reference and at the update function we are going to do exactly the same we just copy and paste it and in the update function I'm just going to copy and paste these two lines as well going to rename it trigger value and I'm going to set that to trigger reference and at the hand animator we are going to use the trigger so just remember to copy it trigger and we have to change this to trigger value one last thing we need to do go back to Unity select the left hand prefab and we have to we have to have a reference for the trigger button we're going to use the xri left hand interaction activate value so just select that and and start play mode uh both of the animations working perfectly and if I use the trigger and the grip button together it's just going to grip so if you want to animate the right hand as well uh the easiest way is just duplicate the left hand controller rename it right hand controller select the right hand prefab and drag it to the controller field double click it and we already have the grip and Trigger parameters so open the blun tree and the blun tree we are going to change the Motions to the right hand animations so to do that just open up these fpx files and the first one is the idle then we have the pinch animation and we are going to use the right hand animation in the last two positions as well okay select the right hand prefab we need to add the script we just made so add the hands animation script and drag the animator to the hand animator field and again on the grip and the trigger we just need to select the XR right right hand select value and for the trigger we need the right hand activate value and that should be it and that's it both of the hands are working perfectly now so in this video we are going to move with our character so to do that click on the XR origin we need to add the component called Locomotion system and drag the XR origin to the XR origin field we also need a continuous movement provider and we have to select the action based one so here we need to reference The Locomotion system so just drag it to the system field and in the bottom we need to reference an input action we need to specify which joystick we want to use for for the movement personally I like the left hand joystick so I'm going to use that reference and here I'm going to use the xri left hand Locomotion move action so I select it and here I'm just going to leave the right hand move action as blank we also need to be able to rotate in the word so we have two options what we can use one is the snap turn provider and the other is the continuous turn provider personally I prefer the snapt provider but you can use the continuous turn provider if you like that I'm just going to add both of them so I can show you how to properly set it up in both case you have to select the action based one so start with the snap turn provider we need to drag The Locomotion system to the system field and at the turn amount we can specify how much you want to turn and at the turn amount you want to specify in degrees how much you want to turn with each input action so I'm just going to decrease it to around 30° and in the bottom I'm going to use the right hand snap turn action so I'm used the reference so here I'm just going to select the xri right hand Locomotion snap turn action The Continuous turn provider we have to reference The Locomotion system you can set this turn speed to whatever you like and again I'm just going to use the right hand turn action they here I'm just going to select the xri right hand Locomotion turn action okay in my case I'm just going to remove the continuous turn provider CU I don't want to do both of the action so in the scene we need some objects so the movement will be much more visible so to do that I'm just going to use some of the polygon assets so go to its folder prefabs and I'm just going to use the crates I'm going to increase the Y position to 0.5 and I'm just going to make a few copy of it and also I'm just going to stack it on top of each other and start play mode as you can see I can turn with my right hand joystick left and right and if I use the left hand joystick and can move around if you you move forward you don't have any collider on you so you just go through the object so to fix that we are going to need a another component called character controller and as you can see right now it's in the ground that fix that we are going to increase the center's y position to to one right now the radius is a little too big so we are going to decrease it to 0.2 just to show you another problem I'm just going to increase the Y position of the character and enter play mode as you can see right now we are a little high but if you move forward a little you instantly fall to the ground right now we can we can collide with objects in in our scene as you can see if I even if I'm crunching the collider stays the same height the first if you want to fix these issues uh at the continuous move provider uh you can see a gravity application mode and right now it's only calculating the gravity when we are moving as you can see if we are not moving when the scene starts we are just levitating in the air and when we start moving you just instantly fall to the ground so we have to select the imediately option so it's always going to calculate the the gravity the other thing so if we want to be able to Crunch and move move under the object we are going to need another uh component which is the character Controller driver here we need to select the XR origin for The Locomotion provider I'm going to set the minimum height to 0.3 as you can see if if I'm crunching the collider the collider's height is following my head so now I can go under these objects so that's it about the continuous movement now why don't we make our environment pretty I'm just going to use the polygon assets to create a simple scene and advise you to do that as well try to be creative as you want to be so see you in a [Music] bit [Music] so this is the end result as you can see I'm not really good with level design but this will do the trick so if we want to grab objects uh we basically going to need two things so we need an object that is interactable and also we need our hands to be able to detect that we are grabing something because at this point if we are if we are pulling the trigger it's only doing animation so first we need to make our hands to be capable of grabbing object so to do that just select the left hand controller and right hand controller we are going to add the XR direct interactor so the script also needs a collider so it's going to detect is the interactable object is in within its collider so to do that I'm going to go with a sphere [Music] collider so right now it's too big so I'm going to reduce the radius to 0.1 and what's really important it needs to be a trigger so I'm just going to check this box first I'm just going to create a cube reset its transform and I'm going to set the scale to 0.1 on all AIS so just put it on the table and I'm going to add the XR grab interactable component to this object as you can see it's automatically added a rigid body at the script the most important thing is the movement type you can set it to three different one first is the instantaneous the kinematic and the velocity track in so I'm going to demonstrate what's the difference between these three so just duplicate this Cube twice and I'm going to set the second one to kinematic and the third one to well truck in and I'm also going to apply a material so we can differentiate between the three types so to do that I'm just going to use the polygon starter pack materials plane and I give the first one red color second one blue and the third one this yellowish black I know it looks weird but it's just for demonstration and now just start play mode so first as you can see I can grab these objects and now let's see the differences so I just grabbed the instantaneous and as you can see the movement is really smooth but you can move through any object so it doesn't interact with any physical object this is because uh the instantaneous is disabling the rigid body so the second one is the kinematic and this won't collide with any object that doesn't have a rigid body so this crane right now only have have a box collider so it doesn't collide with it so the last one is the velocity truck in and as you can see it does collide with the with the crane so you can ask what was the difference between the kinematic and the instantaneous well if you grab the instantaneous and you try to move the other cubes as you can see it behaves really strange but if you do the same with the kinematic it will behave as expected it's also true for the velocity tracking okay so that's the difference between the threee movement type so now I'm going to delete these three cubes I'm going to add three prefab the first one's going to be a pistol the second one's going to be a shield and the third one's going to be a bird so I'm just going to rotate them okay and I'm going to select all three of them and I'm going to add the XR grab interactable or two or three of them so on the pistol I'm just going to leave it instantaneous The Shield I'm going to change it to kinematic and the SW is going to be velocity tracking okay and I start play mode now so let's grab the pistol and as you can see the position is really out and it's going to be the same with the shield and the thir so that's one issue the other if you grab the shield and you move it closer to your body and you try to move it's going to have this strange effect so we just exit play mode and we are going to fix these issues so start with that strange Behavior the reason is why it's happening is because on the XR origin we have a character controller which is a collider and it's colliding with the shields collider so what we need to do is just disable the collision between these objects so we can do that using layers so I'm going to create two layers one of them is going to be [Music] interactables and the other is going to be player I'm going to select The Shield the pistol on the third and I'm going to change its layer to interactables and I'm going to change it to all its children I click to the XR origin and I'm going to change its layer to player but this time I'm I'm not going to apply it to all of his children because if we apply it to the left hand and right hand controller it won't be able to interact with these interactable objects so next and we are just going to edit project setting and here at the physics tab the bottom we are going to disable the collision between the interactables and the player so I'm just uncheck this box so just start play mode and we check if it's working so I grab the shield I move it closer to my body and I just move around without the strange Behavior so next we are going to fix the issues of the position of the grab so if you select the pistol at the XR grab interactable you can see a Feld called attach transform so it's basically a position that it's going to be attached to so we can modify this position based on our needs so to create one I'm just going to right click create empty I'm going to call it attach transform and I'm going to it to this attached transform field okay right now it's not going to do anything cuz we need to find it the position what we want and it can be real tricky because on play mode it doesn't update real time so just drag to set the right position but there's a real useful trick that we can use and this is the dynamic attach feature it basically allows us to grab any object at any position and it's going to to be that position that we are attached to I'm just going to demonstrate it with the bigger object so I'm going to enable the dynamic attach on the shield so if we start play mode so as you can see now I can grab it any position I want to and because if I grab it at any position and if you check it out in the inspector you can see there's a leftand controller Dynamic attach transform has been created so I can copy these components and paste it back to my attach transform and after if I disable the dynamic attach I'm going to be able to grab it at this exact location every time when I'm grabbing it so let's use this feature so I'm going to select the pistol the shield and the SD I'm just going to enable the dynamic attach and I'm also going to add each of the mo empty attach transform component okay so just need to do it one by one so start play mode and start with the pistol so I'm just trying to grab it at the right position yeah that looks decent so I'm just going to hold the grip and in unity you can see the dynamic attach transform just copy and exit play mode and now we can paste this component values so if we go back to the pistol and disable the dynamic attach and start play mode so if you grab the pistol it's going to have this attached transform position so let's do the same with the other [Music] two so let's try it out so start with the pistol yeah perfect and the shield yeah looks all right and then the third perfect it might not be visible in the video but if you move the thir and the shield it has some jittery movement and the instantaneous pistol is really smooth so the reason is because the instantaneous removes the rigid body so its position is being updated in the update function or the kinematic and the velocity tracking position is updated in the fixed update function which means unit's physics update is set to a certain value so you're always going to update that much which means basically it's frame rate independent so if we check it out in the edit uh project settings time and you can see the fixed time step it's 0.02 which means 50 calculation per second and the quest in my case right now at 90 Herz so if I set it to one divided by 90 and start play mode it's going to be as smooth as the instantaneous but bear in mind this is really heavy calculation so it so it can slow down your game significantly so I'm going to say back to the original settings so we are going to start with a simple one we are going to add the heptic feedback to our controllers and with the XR interaction toket it's quite easy to do we only have to go to the XR origin so the left and right hand controller and at the direct interactor open the haptic event and based on when you want the heptic events to happen just take the event when you want it so in my case I'm going to use the unselect entered and here you can set the heptic intensity and the duration so I'm going to set the heptic intensity to all the way up to one and duration I'm going to say 0.08 seconds then start play mode and let's try it so now if I pick up an object I'm going to feel the heptic feedback and yeah I do and of course you can play with these settings and get what's the best for you okay next we are going to do a bullet and we will be able to shoot with our gun so to do that just go to the hierarchy right click uh 3D object and I'm going to create a sphere I'm going to reset its transform and change the scale to 0.05 now I'm going to to create a material so first I'm going to create a folder material and here I'm going to create a material and call it bullet yeah I'm just going to change the color to red and I'm going to apply it to the bullet I'm going to rename it to Bullet as well and back to the assets folder and I'm going to create a prefab folder okay I'm just going to drag the bullet to this sper and now I can delete it from the hierarchy okay so to be able to shoot we need to add a grip to our pistol so what we are going to do when we grab the pistol and and we pull the trigger on the controller actually we are firing an event uh which is the activate event if you remember in my previous video we checked out the the bindings of this new input system so we are going to fire an activate event so here we are going to call that function of course we need a c script for it so just create one so I'm just going to create the fire script and if I open the fire script script we are going to need a reference to our bullet object so so I'm just going to create a serialized fi private uh game object I'm going to call it bullet and if we go back to Unity now we see an empty field so I'm just going to drag the bulletproof up to the bullet field okay in the C script I'm going to create a public function so our XR grab interactable script can access it so let's create a public void I'm going to call it fire bullet and in here first I'm just going to instantiate the bullet so as you can see in the instantiate function we are going to need a object what we are going to instantiate a vector free position so where we going to instantiate and also a rotation a quan rotation which means what rotation we want it to be instantiated so to have this position and rotation we are going to need a spawn point so go back to unit and open up the pistol and if we open it up uh we just need to create an empty object set it to local so I'm just add adding an empty game object I'm going to rename it to spawn point and if I change the coordinate system from Global to local I'm going to be able to move it inside I'm going to move it to in front of the pistol it's important to not put inside because it's going to collide with a pistol so I'm just move it slightly in front of it okay now we got the P point so I'm just going back to the script and I'm going to create a reference for it so again serialize field private and this time it's going to be [Music] transform and I'm going to call it spawn point so in the instantiate function now we can add the uh bullet so this is going to be the object where it's going to um instantiate and the position is the spawn point. position and we are not changing the uh rotation so it can be spawn point rotation if we go back to Unity select the pistol now we can add the spawn point to the spawn point field okay the only thing that left us to do is on the X grab interactable select the interactable events and on the activate event you just drag the fire script to the activate one and here go to the fire so it's going to be the script and here you can see the fire bullet function so select that one and if we start Unity so if we press the trigger as you can see we are instantiating the bullets okay but as you can see they are just floating in the air so to actually to do something we need to add physics to it so just select the bullet prefab and adds a component called rigid body this basically going to enable us to use unit's physics system so if we go back to the script so if you want our bullet to have a speed then we need to store it in a variable so this is going to be a game object I'm going to call spawn bullet now we will be able to reference its components so if we get the rigid body then we can add uh speed to it so let's do that so start spawn bullets. cat component and we going to have the uh rigid body component uh do velocity and here we need to specify a direction and a speed variable so the directions will be the spawn point the forward this is going to be the Z axis and we need to multiply it by Bullet speed so I'm just going to call it bullet speed and I'm also create a variable for it so serialized field private it's going to be a float value and I'm just going to copy and paste it at first I'm going to say it to 10 and we can adjust it in the inspector so let's go back to unit and here we have the bullet speed the bullet and the spawn point all set up so again if uh this speed won't be enough we just need to increase it here so hit play mode and if I pull the trigger it does work so one more thing on the bulletproof AB we need to uh change the Collision detection to continuous uh this is because it's a fast moving object and this will improve the detection of the bullet when it goes to for example to the crane and bumps into it and it will reduce the number of issues when it just go through the crane without uh colliding with it so one more thing um so in our script there one more thing we should do is we need to destroy our instantiated bullets after let's say 5 seconds so to do that just call a destroy function we are going to destroy this spawn bullet and and let's say 5 seconds after it's instantiated okay just start play mode then let's see if I fire a bullet then after 5 Seconds it should be destroyed yeah and it's gone so one last thing for this video um if we grab an object with one hand it's perfectly fine but if you grab it with the other uh the position is off this is is because on each object we need to uh attach transform for each of our hands so to do that we would need to attach transform here and we should be able to tell which hand is actually grabbing this object luckily we have a way to do that so I'm just going to create a new script called fixed XA grab interactable let's create that script and open it up so we need to use the XR interaction to K here so using using Unity engine. XR interaction. toolkit and here if we change this from monob Behavior to XR grab interactable then we will be able to overwrite some of its functions so I'm going to overwrite the on select ENT and here this space is basically mean if we change anything here after that runs it's going to continue the rest of the function so here we are going to need two variable for each um attached transform so I'm going to create a serialized field private transform I'm going to call it leftand attach transform and I'm just going to copy and paste it and rename it to right hand thatch transform okay let's go back to Unity and we are going to differentiate our hands by using tags so let's create two tags one's going to be the left hand and I'm going to name the other one right hand so back to the left hand controller and set it to left hand and on the right hand controller say to right hand so back in the script if we we grab it with the hand with the left hand tag then we are going to use the left hand attach transform and and with the right hand tag we are going to use the right hand attach transform so we are going to need need an if statement so let's start with augs interactor object do transform do compare tag and if it's uh left hand then we are going to set the attached transform to the left hand attach transform attach transform equals left hand attach transform and if the AR the interactor object transform. compare tag is right hand and we are going to set the attach transform to the right hand attach transform and after this check the whole function is going to work as before so let's go back to Unity so on the pistol we just have to delete the XR grab interactable because we already have this fixed XR grab interactable again we have to select the interaction manager and down here you can see the leftand attach transform and the right hand attached transform so in my case the attached transform is set to my right hand so I'm just going to rename it to right hand attach transform I'm going to duplicate it and I'm going to call it left hand attach transform and what I'm going to do is based on my previous video If you haven't seen that uh check it out just going to select the pistol enable the dynamic attach and start play mode and I'm just going to grab it with my left hand in the right position let's say this is right and now I'm just going to copy the dynamic attach transform exit play mode and I'm going to paste these component values and back here I'm just uh disable the dynamic attach oh I need to rename it to left hand attach transform my bad the fix desar grab interactable I'm just going to grab the left-and attach transform and drop it in the left-and attach transform and the same with the rightand attach transform okay now the dynamic attach uh is disabled so start play mode and let's see so as you can see it doesn't work and it's because I misspelled the right hand and this is exactly the reason why we need to be careful with strings so I'm just going to untag it and the tag I'm just going to delete it and add the new one called right hand and hopefully this works now right let's start play mode again and yeah it just works perfectly now so we also going to need need a Target uh I'm going to use a free asset from the asset store it's a lowly training dummy you can find the link in the description so I'm just going to import that once it's imported you can place it somewhere in our word as you can see we have a pink material so we need to upgrade it to the universal render Pipeline and to how to do that you can find it in my first video or just quickly go to window rendering render pipeline converter we need to take these initialize and convert yeah it works now so right now we are just going to make a simple script it's going to called the target dummy and once it's opened I'm just going to delete the update and start function and I'm going to use a function called on collision enter and what we are basically going to do is if it detects our bullet within its collider then it's going to give us a message so if we want to identify a bullet then we can use uh tags to do that go to the prefer folder select the bullet and we are going to add a new tag I'm going to call it bullet and again I'm going to apply it to the bullet so if we go back to the script then if the colliders trigger will be bullet then it's going to give us a message so if other the other is uh going to be the thing that's going to collide with our collider so other the game object the compare tag and if it's bullet then we are going to write something out in the console so debug.log and I'm going to say I've been hit yeah we can go back to Unity as you can see the shooting doesn't work the reason is because we have a new fixed XR grab interactable script and if you remember we have added the fire script to the activate event so we need to do that again so at the activate event just add one to the list and drag the file script in there and here just uh select the fire bullet function oh and one more thing uh we need to add a collider to our Target D I'm just going to use a simple box collider now and I'm just going to adjust its size say like that yeah as you can see it does work so this is going to be fairly easy all we need to do is go to the polygon stter pack prabs and we need to find the door frame there it is just rotate it a little bit and then I'm just going to drag the door in and now what we're going to do is just uh make it it's a child object and reset its transform now we need to drag it to the hinge position so just give or take and now we can rotate it as a door I'm just going to give it a different material let's go to the materials and I'm going to select the gray one now it doesn't really matter if it's uh colliding or not at this point cuz it's going to be just a simplified version first we need to add a rigid body we don't need the gravity in this case and I'm also going to add something called H joint and this one's basically will allow us to rotate it around an axis so right now as you can see it rotates around the x-axis but we need to rotate it around the Y AIS so all we need to do is just change the axis the X to Zero and the Y to one now if we check it out now it's going to rotate around around the Y AIS there's a few option what we can set and we will get back to it in a minute but first we need a grabbable knob so to do that I'm again going to create an empty game object and I'm going to move it to the knobs position and I'm going to add a sphere collider I'm going to change the radius to 0.1 yeah looks all right again we need to add a rigid body and won't use uh gravity again and we need an XR grab interactable but right now if we grab this one it's going to move independently to the door so we need to fix it position to the door and there's something called Fix joint and we need to grab the door to the connected body and now it should be working I'm going to rename this object to knob and if we start the game just let's see what happens and if I grab it it moves really strange so there's multiple problems with it right now uh first our box collider is colliding with the frames collider so what we can do is so we can do is create two layers and just disable the collision between them so we need to add two layers so I'm just going to create one for the door and one for the frame and I'm going to apply it to the frame I don't want detergent to be to have this um layer and I'm going to change the doors to door so we still need to disable the collision between the door and the frame so to do that just go to the edit project setting pH physic and at the bottom just disable the door and the frame Collision uh second problem is the movement type if you remember in my previous video I talked about these three different movement types if you want to know more just check out in the info card so in a nutshell right now it's in instantaneous movement type which means it doesn't use uh any physics so if we change it to the velocity track in it should work as we want to okay and let's hit play so there is two issues first if we grab the other knob it doesn't work or it just simply snaps to the other one and the second one is the door can move all around the axis so let's fix these two issues as well so I'm just going to duplicate the noob and I'm going to move it to the other knobs position I'm going to check if the collider is okay and that should work and if we go back to the door so on the H joint there's something called limits and and if we use that one we can set a minimum and a maximum degree of limit so if I say to 90° it should allow it to rotate 90° so let's try this one out yeah and as you can see it perfectly works both of the knobs and get stuck when it reaches the 90° limit there's two more thing uh it's something called Spring and motor the spring is basically pulls the door back to its uh zero position and the motor is doing the same but while the spring is accelerating to that position the motor is just reaching a Target velocity and and rotates to the zero position with that speed so I'm just going to demonstrate the difference between the spring and the motor so this is for the um spring as you can see it's accelerat and this is for the motor and it just uh goes with the same speed till it reach which is the zero position so that is the basics of making a door for uh VR environment so first select the target Dy and in the inspector add the animator component open the controller in the animator window first go to the parameters Tab and add two triggers we will need these ones to play the different animation based on if it needs to activate itself or die so call one of them uh activ [Applause] and the other one death so on the base layer we will need three state for this object first we need an idle animation which will be automatically played by the animator then we will need an activate State when we trigger it by one of our action for example walk into a trigger area and finally uh that state when we hit the object so let's start with the idol animation in this tutorial we don't need really any fancy animations so I use a real trick for this one all we need to do is go to the asset training Dy mesh and if you open it the training Dy fbx file and you can see a died and a pushed animation select the died animation and push contrl D to duplicate it rename this one to Idol so why are we doing it if you play the died animation you can see at the end it just lies on the ground so we can use these key frames to use it as an idle State when it's lying on the ground so open up the idle animation and select everything except the last frame and delete it and then select the last frame and drag it to the front save this one and close it up now head back to the animator window and create these states first create an idle so right click create empty I'm going to rename it to idle and I'm going to set it uh the default layer and we can delete the push animation and I'm going to create a new one and I'm going to call it activate and we can rename the DED animation to dead so in the idol State drag the idol animation to the motion field and next at the death stage drag the death animation to the motion field and at last the activate State we will use the death animation again with the little trick in the inspector under the motion field there's a speed variable with this we can control the speed of the animation if we say to let's say 0.5 it will play in slow motion and if we set it to two it will speed up but here's a little trick if we set it to minus one then it will play the animation backwards and if we think about the death animation imagine it backwards it will basically stand up which is exactly what we need at this moment so just drag the died animation to the motion field so so the last thing we need to do here is connect these states to each other so right click on the idle State make transition and select the activate State click the transition line and in the inspector disable the has xit time and set the transition duration to zero it means when we trigger this transition the activate animation will be played immediately under these add the conditions add one to the list and and select the activate tra and this is the trigger from the parameters Tab and repeat the same with the death state so make a transition select the transition line disable the exit time and set the transition duration to zero SEC add the condition to the list and change the activate trigger to the death trigger with all that set up all we need to do is tell the animator when we trigger these events well how can we do that of of course with a c script if you remember one of my previous video we set up a script for the Target domain check it out the link in the description so open up this script this is going to be the target domain as you can see there's the on collision ENT function where if the colliding objects tag is bullet then it writes to the console a message um I've been hit so all we need to do in this case is change the console message to triggering the that state in the animator if you remember in the second episode we did something similar with our hands animation the only difference was that we used float values instead of triggers so here's a little challenge for you stop the video and try to figure it out yourself see you in a bit welcome back did you figure it out did you find another solution let me know in the comments and here's my Approach first we need to reference the animator so serialize field private animator and I'm going to call it uh dummy animator so here just uh Delete the debug. log message and replace it with uh dummy animator set trigger and in here we are going to need the uh death trigger don't forget to watch out for the spelling of the parameters name so better just to copy it just to make sure so just like that and one little thing I would like to change instead of using a bullet tag change it to weapon this is because if I want to be able to slay it with for example with a s it would be strange to tag it as a bullet or if I want to use both then we need another or check in the if statement for example bu you can do that if you like that for me I just like to keep it simple so I'm just going to delete it so if you follow me then don't forget to create a weapon tag and apply it to the sword and also to the bullet next we need to set up how can we activate the dummies so to do that we will need a new function let's call it void activate dummy and here we just need to set the activate trigger again it's better just to copy it from the animator so how can we call this method of course with another C script and we will attach it to a game object where we are going to have a box collider trigger so when the player walks into it it's just going to trigger the activation well right now we have a problem if we have multiple Target dummies multiple places we don't want to activate all of them in the same time so we need to separate them into chunks and activate them in different trigger areas so to do that I will use one of the prefab just for the visual if we go back then go to the polygon starter pack refab here I'm just going to use this plate move it up a little and here at the Box collider I'm just going to set it as a trigger and going to increase its size just to make sure we when we walk into it we are definitely hitting the trigger okay and I'm going to add the new script while I'm going to call dummy trigger and open the script so here just delete the update and the start function and in here we need to store in a variable all the target dummies we want to activate and for that we need an array of game objects so again start with serialized field private game object and here we just need uh brackes and now this is a array and I'm going to name it Target dummies and again we are going to need an UNT trigger renter and for the logic what we will do is if the player walks into this trigger then it will iterate to all of the game object what the array contains and Trigger the activate trigger through the script so again we are going to need an if statement and again use a compare tag so if the colliding object tag is going to be player then we want to iterate through to the array for this iteration we will use a for each Loop so the variable we need a game object and the collections going to be the target domies array and in here we need to call the active acate domy function but right now we can't do that CU this is a private void so we have to set it to public so uh here just Thum is the G component Target Dy do activate Dy and this is the function what that we call in so that's it for the Lo logic you can close this up and in the inspector select first the target dumy we need to drag the animator component to the dummy animator field I'm just going to duplicate it few times and here I'm just going to drag the target doit to the array the ones that I want to activate if I duplicate it and move it somewhere here and delete these ones and drag the other two to this array then it should activate those two when we walk into this one and don't forget the XR Origins tag it needs to be changed to a player now press start and try this out now if I walk into this trigger area these two target demies gets activated and if I use the pistol I can shoot them check it out if I walk back into this trigger it won't enable them again this is because we don't have any transition line back to the idle or activate State and if I walk into this trigger area those them is getting activated and I can use this word to slay them perfect how to make a UI to do that we will need a ray interactor so we need to create it on our hands so to do the let's let's go to the XR origin camera offset and create a new empty object call one of them left hand Ray and the other one is right hand ray okay resets both uh of the transforms and we need a few components first add the XR controller and select the left hand first and add the left hand preset and then the right hand and at the right hand reset okay now we can select both of them and add a xarray interactor also we need an component called XR interactor line visual yeah this one adds automatically a line renderer and we can reduce it with like 05 right as you can see it uh has this pink material which means it won't be rendered so what we need is to create a new material I'm going to call it line material we need to change it Shader the off Blended pre multiply and we select the left hand and right hand aray and add this material to it and as you can see it renders now okay we all that up just create a UI now so right click UI and we are need a canvas as you can see now it's a pretty big and it's because it's in overlay so if I add uh let's say a button to it then if we go to the game mode it always going to show it in the middle wherever we look but that's not what we need we need it to place it somewhere in our word we can do that with on the canvas just change the screen space overlay to word space and I'm going to fix the scale and say 0.001 on all axis reset its position to 0 0 and now it's just a small canvas what we have here all right I'm going to add a slider to it I'm just going to move it up slide change its scale Al I'm going to change the scale of the button as well and move it slightly lower and I'm going to add another one code so I'm just going to add a drop down as well increase its scale and for the final one I'm just going to create a background to it so I add a panel and change the color to something grayish and increase it f a Yu as you can see now it's all the objects are behind this panel but if we change the order in under the canvas first one it will go to the back position right on the canvas we need to delete the graphics rayter and add something called track device graphic rayter and on the event system we need to remove this Standalone input module and add the XR UI input module here all we need to do is Select uh preset and add the only one we have here and that's it I'm just going to move this canvas out of the way and and rotate it a little and now if we press play mode we can try it out as you can see it does work you can select any option press the button and toggle the slider also right now we can also we can do a distance grab but right now it's not something we want so and also we don't want to see this line renderer all the time so how can we do that but it's Prett pretty easy to make just select the right and left hand rate at the XR interact to line visual if the it's invalid color just change it to change the alpha to zero so it will be transparent on both of them we can change this blocked one to zero as well we won't need it right now and on the valid color I'm just going to change the for slightly at the start and slightly on the end so next one we only want to use it on the UI system so to do that we can select the layers we want to use it on so right now it use all the layers if we select nothing and then select the UI and just the UI only then we can hit play and we can see if it works yeah right now we can see the line renderer but if we move it to the canvas it shows up also if we move it here it won't work so what can we use it for obviously we can use it for exit the game on the slider we can set the audio level or we can set for example the speed of our character and on the drop down we can select if we want to continuous turn or snap turn so right now just do that one I'm going to delete the option C and and rename the option A to snap turn and the option b to continuous to okay and we need to create a script to control it and we just need to add to the XR origin The Continuous turn provider I'm just going to move it right under the snapt provider on the preset I'm going to add the XR default continuous turn provider the system and just select the XR origin and we we probably just want to turn with the right hand joystick so I disable the left hand so by default I'm just going to disable the continuous turn provider select the canvas and and I'm going to add the new script to it and I'm going to name it drop drum script it doesn't really matter at this point and what we are going to do is reference these two scripts the action based snap turn provider and the action based continuous turn provider I just delete the start and the update function so I add two reference um a snap turn provider and the continuous turn provider I'm going to create a function a public function this time because we are going to use it in the drop down element I'm going to need a int integer for that one and I'm going to call it index if we go back to Unity and select the dropdown we can add a script to it going to drag the canvas there and on the dropdown script we have a turn provider select what we just created and also you can find it down here turn provider select with an integer if we want it to work properly we need to select the the top one it's really important and right now if uh this value is zero then it's going to select the first one the snap turn and if it's one it's going to select the second one if I have multiple one then it goes like 0 one 2 three okay so we just basically need an if statement here so if the index equals zero then we want the snap turn to be enabled and the continuous turn to be disabled and if the index is one then we want it the other way just like that oh yeah and on the canvas we need to drag the XR origin to both of them and it's automatically going to select the action based snapt provider and the action base continuous turn provider so let's hit play yeah and as you can see it works perfectly yeah one more thing on the button just the text and we are going to call it exit and here we can make another public function and we are going to call it exit the only thing we need in this function is uh when we want to create the game is application. quit obviously we can't quit in the editor but if we want to check it we can do a debug. log it's exiting or quitting okay then select the button uh add one to the on click and drag the canvas there s the drop down script well I should have named slightly different but it doesn't really matter now and select the exit script okay and we can try it here and if I press the exit button on the console you can see an exit message so it it does works okay I'm just going to delete the debug. log and close the script and now if we want we can create a simple simple scene to it and just add a few other game objects like um whatever you like from the from the bre tabs so I'm going to do that and see you in a [Music] [Music] b oh yeah and one more thing uh select our Target D open it script we need to reference the Box collider so once we hit it and it plays its death animation the Box collider is still stays there so we need to disable them all we need to do is uh get get the component and when it's hit we just disable it so that's that's all we need to do save it and close it up and this is the [Music] [Music] game then how can we build it and make a proper game well first we need to go to the edit project settings and depending on what we want to build to if it's um PC it's already set up but if you want to directly install it to the let's say Oculus Quest 2 then we need to enable the Android settings again just enable the open XA and under the open XR just add some interaction layers like we did in uh first video so touch controller Oculus profile okay so if you want to build it to PC then go to file build settings and right now it's on it's on PC now and just press build and it's going to build our scene we need to make sure the SC SC in build is the one we have right here is selected if we want to do that to Android we need to switch devices so just select Android and switch platform right uh on the device we can select all and just head to the settings project settings layer and here we will have a few settings so that's it for this tutorial series if you liked it then hit the Subscribe and like button and see you in the next one
Info
Channel: GameDev Blueprint
Views: 11,825
Rating: undefined out of 5
Keywords: Unity, unity3D, VR, Game Development, Virtual reality, indie, Beginner, Tutorial
Id: TJ8yk5vVwI0
Channel Id: undefined
Length: 80min 12sec (4812 seconds)
Published: Tue Sep 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.