HOW TO START A CUTSCENE FROM A TRIGGER WITH C# IN UNITY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys Jimmy Vegas here and then this mini UNC tutorial I want to show you how you can play a cutscene in your game just by entering a trigger don't forget click on subscribe and click that Bell icon as well to stay up to date with everything I upload on video game development on my channel with that in mind let's get to work so a cutscene it doesn't really matter how you set your cutscene or whether you use a camera with animation whether you use a timeline where whatever you use as long as it's either like a video that plays or a camera it's going to work either way because the whole premise of this is our player in this case first-person controller but it will work with a third-person controller as well when they enter the trigger we want the cutscene to play now I have a camera set or that just has a simple animation that just goes all the way over here and that would represent our cutscene so we want our player to walk over here and then that cutscene trigger so there's the controller and there's the camera I'm going to disable the camera or PS I'm going to on ticket because we don't want that to play at the same time as the player being active and to get this working properly we need to firstly create a c-sharp script so right click create c-sharp script will call this cut scene enter and then we need to open that up in a visual studio it's probably going to take some time because Visual Studio tends to be like this when you first start it up so I really should have opened it before I actually did the tutorial but either way I'll use this time to explain how this is going to work so we're going to have a section on our level that where we walk into it that will act as the trigger and then it will execute whatever code that we write in this script so we're going to basically say when a trigger enters it then we're going to switch off that player and switch on that cut seam that cutscene will then play out and obviously you can go further after that you can change how it's done but that's entirely up to you I'm showing you the mechanics of how you would trigger a cutscene in your game so it's loaded up now and what we're going to do is get rid of void star and void update because we do not need them we're going to have two variables the first being the player the second being the cutscene camera so public game object D player and obviously like I said it'll work with first person and third person then public game object cut scene cam semicolon then after that we need to have the method on trigger enter so void on trigger enter and it doesn't need to be private that's fine if you're dealing with tags that's completely fine as well in this scene because there's nothing else that will collide with it there's no purpose to be using tags however if you have a bigger scene you probably would be better off using tags either way all we need to do is ontriggerenter we need to say cutscene camp dot set active true semicolon and then the player dot on set active false semicolon and save so I always feel it's a good idea to turn a camera on before you turn the previous camera off reason being is you could introduce a little bit of flicker per se because think about it if your turn the camera off before you turn another one on there could be a moment there that would prevent any camera for rendering so I always like to turn something on before it goes off and that's exactly what is happening here as soon as we enter the trigger we're turning on the cutscene and then turning off the player theoretically in the same frame but just to be sure I always do it first so head back into unity and now we just need to set up that trigger so I think we're going to have the trigger panned across here so if we take a look at where our play is in relation to where we want the trigger we mainly to move the player back actually now I think we should be alright so I'm going to bring in the trigger here so game object three the object cube and then let's place this cube in the scene where we would want the trigger to occur in this case about there and let's change the scale so as it goes all the way across roundabouts that should do it obviously you could be more precise in your game I'm just doing this to speed things along a little turn off mesh renderer because we don't want to be able to see any texture or material on there I should say and then make sure you tick is trigger on the collider that's crucial because that will obviously make the trigger active then we just need to drag and drop that cutscene script onto that trigger cube you've renamed the cube if you want to and then just set the two variables the first one being the player and the second one being the camera and now let's press play on as soon as it starts we should be able to control our player no problem as intended there we go so if we walk this way now we should trigger that cutscene there we go now obviously this cutscene will loop round and round however you would just put extra coding in to say when it's finished you can resume the player and basically all you need to do from there is use and co-routine I was go saying ienumerator voice that's a KO routine and to be honest I'm probably gonna get questions about that aren't I so let's do it now so the cutscene is actually 10 seconds long so what I'm going to do is turn off the trigger on this so this dot game object dot getcomponent and make the brackets box Collider up close bracket door enabled equals false and like I said we need to set a ko routine and it's actually real simple it's just something you take another minute to write I enumerator now let's just call it finish cut up closed bracket open curly bracket and then yield return new wait or seconds and then 10 because I've got scene is 10 seconds long however long your cutscene is you would place in there after that 10 seconds of the player dot set active Drupal and cutscene kam dot set active false and then the last thing to do down here would be to start co-routine and then the name of the co routine finish court o close bracket close bracket semicolon and save so now we've gone that one step further and after we played the cutscene we'll be able to resume play no problem I knew was going to get questions about it so I figured why not just write it instead of being a little bit lazy so once we're back in the game let's walk this way trigger that cutscene as soon as it finishes eventually we'll be able to resume play with our player like so so you lose it a quick little flash there kind of reset don't worry about that that's just the timing of this here so if I change that to probably nine seconds you won't get that glitch again that's not really glitch this is the way it works that's all you would need to do there so guys I hope that's helped and yeah if you need to know any more let me know in the comments below and yeah thank you very much for watching guys
Info
Channel: Jimmy Vegas
Views: 76,899
Rating: undefined out of 5
Keywords: unity tutorial, unity tutorials, how to make a game, brackeys, jimmy vegas, unity3d, game development, c#, programming, coding, video game, first, noob, assets, beginner, easy, how, to, learn, tutorial, tutorials, tip, game, development, develop, games, make a game, for free, survival horror, unity 3d, fps, gta, grand theft auto, unreal engine, indie, beginners, asset, model, texture, models, textures, howto, course, series, basic, basics, how to make, getting started, level design, introduction, intro
Id: pru5sx_hqeE
Channel Id: undefined
Length: 8min 30sec (510 seconds)
Published: Thu Dec 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.