HOW TO MAKE A VR GAME Part 6 Targets

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay now now my mics on hello and welcome to another vr tutorial in this one we're going to carry on from last week where we left off and last week we had a look at actually creating our gun so when we pulled trigger it raycast into the scene and we got some feedback in the console about what was happening now we're going to link up our target so that the target knows when it's been shot and to carry out some actions let's dive right in so before we get stuck in i just very quickly wanted to go over what it was we did last time for those of you that may not have been following along with these tutorials so last week we had a look at hooking up our gun and uh what we did is we created this gun script here we put on the root of our gun object and this was listening for certified in visual studio makes more sense so here is the um the gun script as it stands uh and all we're doing at the moment is on awake on the way we're just getting hold of the audio component if it's not there we're going to add the audio source on so that we've got um some an audio source to play our gun sounds and then on the neighbor and on disable we're just listening for the trigger pulled on the controllers and when the trigger is pulled uh it's gonna figure a method called trigger pulls so it triggers uh and it's gonna send a haptic impulse to the controller and it's also gonna play some audio which is our gunfire sound and it's gonna fire raycast into the scene and this is a method on its own just here just below and all this is doing at the moment minute is firing a raycast from the barrel of our gun out into the scene and it's looking to see if we've hit the target layer and if we do then it's going to show a debug log message in the console so let's go ahead and take a look at that in action in vr here we are in our game view we're going to grab the gun into that a couple of tutorials ago i pull the trigger you get the audio and the haptic rumble and then also what's happening is raycasting getting confused about what happens you can see a virtual hands on my non-virtual and here when you pull the trigger it's right from the gun out into this and then join the debug message console and you can see there and the bottom of the console i've got hit target that's great but now in this one we want to make it so when we shoot the target target knows it's been shot and then does some actions and to do that we're going to use an interface also it's probably worth going over a couple of you asked me didn't mention that you've had some hand on grabbing the gun so your hand animates normally and comes back fine when you press the grip when you're going to pick up the gun and then release it it's getting stuck like this won't open up again um i found the reason for that there's something on the on the right hand controlling the left-hander controller you've got the direct interactors you go ahead and uncheck hide controller on select and then if you run it again oops there we go hi controller on select then go and run it you'll see that when you let go the hand will spring back into support there obviously something going on there with the direct interactor and i hope some kind of update i don't think it's something we've done in our scripts and done anything different but if we do want to decide we want to hide the controller models later on and then we can go about and write a script to do that ourselves but let's get started with interface and then we can have our top our targets react to being shot so you may have heard me mention that we're going to use an interface to connect up our gun and our target so that the target knows when it's been shot and many of you might not be familiar with using interfaces and that's okay we're going to go over it as i should much as you can do make it as clear as possible but a way to think about an interface if we have a common set of functionality we can group that and bundle it all within an interface an interface on its own doesn't really store any logic code or any kind of values instead it's up to the inheriting class to use the methods laid out in the interface and then you can generate your own code inside that class so you can think about an interface as being like a contract that states that whatever class inherits from our interface has to have a particular set of methods let's go ahead and we'll create our interface for our target so anyway in your scripts folder i'm gonna go to my targets folder i've got one just made here i'm gonna right click and i am going to go and create a c-sharp script now naming here with interfaces is a bit different to your normal standard class you want to start it off with a capital i we can call this target interface you don't need the interface on there i just put it in there to make it clear what it is those of you that may not have used them before i'm gonna go ahead and double once that's compiled and go ahead and double click and open it up in visual studio now our interface doesn't use any of the normal name spaces like our normal classword so we can go ahead and remove everything and the syntax for getting this started is going to be public when you want to write interface and then the name of your interface which is i target you could very easily just call this i target and drop a line and open nuclear bases now it's here where we need to consider what are the common elements of our targets we need to know when each target has been shot so a good name for a method might be void so we just want to list some methods that all our inheritance classes must have so it's going to have a void targ target shot and we also want to play an animation so we could say void hey animation get on my typing today my animation and then the last method we want each of our targets to have is going to be when it has to play some audio so we can say void a audio what we've done is we've created this contract that's interfaced that says that whenever we inherit from the i target interface that class must have a target shoot method it must have a player animation method and it must have a audio method let's now hook this up in an actual script for our targets and we can show you how how you can link it together to our gunfire script so back in your targets folder let's right click and we're going to actually create our target script this is the script that's going to sit on the target and go ahead and call this target controller and then what we'll do is we'll double click to open up in visual studio so now this is the target script that's going to actually sit on the object that we've shot and we need to connect up to our interface and to do that we're going to go to where it says the public class target controller at the top and then mono behavior and we're going to put a comma here i'm going to write i target interface and now everything seems okay and that's good but you should get a red squiggly line that's not nothing to worry about it's just warning us that our current class does not implement the interface so we're not using any methods that we've just created from our interface target shoot play animation and play audio gonna fix this you can hover over the squiggly line control will stop and then you should get the implement interface box pop-up and go ahead and implement it like so i'm going to remove the start and update you can see here by implementing this in interface it's actually created three methods the play animation method the play audio and the target shot method now here's where you can you might be able to see the power of interfaces now starting to make sense because what we could do is we can make various different classes and scripts that all inherit from my target interface but all contain these methods but in each method in each class we can do something different inside the method although heritage inherits from the interface you can actually do custom functionality in each class in it inherits from i'm just going to go ahead and jig this around a bit because the most important one we want to deal with is the target shot so i'm going to go ahead and cut and paste it to the top like so then save to make this a bit bigger for you it was going on so this is great we've inherited from our interface and we're going to say that our class is going to use these three common methods from our interface so now we can go ahead and do something so once the target has been shot we can save destroy and then brackets and just put in game object and let's save that for a minute and it doesn't matter that we haven't put any code inside our play animation you can just write like to do whatever you want in there really you can just leave it empty so this is going to sit on our target so when our target has been shot it's going to destroy itself so let's go ahead let's minimize that and we'll drag our target controller onto our target in the scene so you see we've got target drop it on there and there's a script that sits on our target object called target controller but how does our target know when it's been shot well that's up that's going to be handled by the gun raycast we're going to open up that script tweak it a little bit and then we'll give it a test let's go and find our gun in the scene which is our gun parent scroll down and double click on the gun script okay so when our trigger's pulled haptics gunfire sound fires are recast firing of the raycast happened here in this statement and what we're doing is we're raycasting from the barrel position out into the world in the forward direction and then we're storing whatever we hit in the out hit here and we also we are also checking that the object that we've hit is in our target layer which we set up in our last video and you know we know this works because we're getting a debug message in the console but rather than a debug message let's find out if the object we've hit is inheriting from our i target interface we need to test to see whether this hit transform has got our i target interface on it so we can say if it dot transform dot get component and we want to know if it's got the i target interface which has because what our script the target controller is inheriting from that so therefore it will have it and then you want your brackets and we want to check if it's not null so if the target is inheriting from the i tiger interface then it's going to do this stuff which is going to be hit dot transform dot get component we want to get hold of the i target interface this is where we're going to start leveraging the power of the interface now and after our brackets but full stop and then here you're going to see the list of all your methods contained within that interface you've got target shot play audio and play animation well our target's been shot so we're going to tell our interface that the target has been shot so we're going to go ahead and click on there put in our brackets and add a semicolon if our object that we've hit hasn't doesn't inherit from our interface it basically does nothing so you could put a debug in here if you want to oh or you don't even really need the else but i'm just going to go ahead and write here debug.log not in aerating from interface so that may be a bit confusing i'm just going to give it one more time we break cast out into the scene hit something so we store it in hit and then check that transform that we've hit to see if it is inheriting from the interface and if it is then it's going to call the target shot method on that component and if it's not then it's just going to debug to our console so once we've called the target shot method and our interface our target controller because it inherits from the interface is going to go aha target shot and it's going to do this method which is going to be destroy game object and because it's sitting on our target it's going to destroy the target so let's go back into visual enter unity wait for it to compile and let's give it a test so here we are in our scene we've got the gun now go ahead and shoot the target and you see the interface is called raycast has detected that that object had the interface on it as our target controller inheriting from face the target shot method was called on the interface and the token controller then turned the game object off that's how we've linked it together we've linked the guns to our target through the raycast then checking for an interface on our target that we shot pulling the appropriate method let's quickly jump back into our we click on our target our target controller here and double click the script so now we've got that bridge between our gun and our target you can literally do anything now once we've shot the target so we don't necessarily need to destroy it we could do anything because from once the target's been shot which connects our raycast to our target we could say play animation so we could call that method within this class so we could say play animation once it's been shot that's what we want to do so it's going to come and play this animation we also probably would want to play the audio so we could say play audio and then you might also want to when we get to it we'll be telling um the game manager that the target's been shot and to assign the appropriate amount of points so you probably do all that here so we've got some to do's here and this is going to be probably next week's tutorial so they don't get too long and too overwhelming i think it's probably a good place to leave the video here we've actually done quite a lot we've actually now set up we've had a look at what interfaces are and implemented them now in our game so that when we shoot a target that interface is used to then trigger the target shot method in our target controller class so next week we'll take a look at actually connecting up our animations and audio to this class and we'll look at extending its functionality a little bit because we need to consider that when a target's been shot it needs to have its animation play out and scores assigned before you can shoot it again what you don't want to do just keep shooting the target and the animation just keeps going and going so we'll put in a little delay in there as well but we'll take a look at all that next week so so just as a quick example of what we when what we'll have at the end of next week i'll probably have a little bit more than this because i'll start doing the 3d models for the um the targets but and this is the the high res patreon members scene um but i've been testing around in this one but this will give you an idea of what we're working towards next week now that we've set up our interface battery is fine so then when you pull the trigger you get a sound the target reacts plays out a little more a little animation as you can see once you've triggered it you can't trigger again so it's ready and john wick practice there we go i really hope you enjoyed this week's video well it doesn't feel like it we've actually got quite a lot done we've now connected up our gun to our targets using an interface so now our targets react to when they're shot if you're learning some stuff and finding these videos you thought would be great to have you along and on board as a subscriber and also leaving a like on the video if you've enjoyed it but that's it for now i'll see you next week
Info
Channel: Daniel Stringer
Views: 320
Rating: undefined out of 5
Keywords: unity, game, development, tutorial, VR, XR
Id: eg4QTskfnX8
Channel Id: undefined
Length: 17min 18sec (1038 seconds)
Published: Sun Jun 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.