Bolt Tutorial - Doors & Portals in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm jim hommack on youtube and discord and i'm here with a tutorial all about doors in this video we'll cover triggering animations on our character rotating objects with state machines triggering animation states like pressing the button for the drawbridge here creating custom events and using those custom events to trigger animation states like these nights and finally portals using triggers to go to other locations or scenes so uh that's everything i could think of uh that you might wanna do with doors so let's go ahead and get started below the video you'll find a package that has everything you'll need to follow along and i've also included my completed macros the first door the gate has a hinge joint on it and i've adjusted the angle limits there's also a rigid body and a couple of colliders if you press play you can see it in action we don't need to script anything on the door but let's use bolt to get a push animation on our character to trigger the pushing animation i made a collider on the door and toggled it as a trigger to eventually trigger the animation so select the third person character and add a flow machine for our character animations click new macro and save it in your macro folder i'll save mine as player push animations underscore tutorial that way i'm keeping my previous completed graphs i'm going to bring up the animator window and i've already set up the animator for you when we come up to the door we want to transition to this pushing animation state and stay there as long as the player is at the door hover your mouse over the graph and press shift and space to go into full screen and delete the start and update units right click create new unit and search for ontrigger stay and also uh right click to make an on trigger exit unit the player animation should stop when uh they leave the door in order to find out if the player is in the trigger drag out the flow and type game object compare tag connect the collider to the game object input the tag that we're looking for is the tag door i've already tagged the gate for you i'll type door exactly as the tag is with capital d ctrl d to duplicate the unit so we also check the trigger we're exiting on the compare tag unit we can see that we get an output of a bool saying yes it's tagged door so let's make a graph variable for that call it trigger push make it a boolean and hold alt and left click drag because we want to set this variable that is we want to set this variable as true when we're staying in a trigger tagged door drag out the flow and create an animator set bool unit we want to transition to the pushing animation using this true false parameter is pushing that's on the animator so if true uh we'll go to the push animation and if false the transition will go back to the grounded third person controller walk run animation state left click and drag that out to get a get variable of trigger push we're going to use our graph variable here to set the bool transitions on the animator that is the bool parameter is pushing if we're exiting trigger push should be set to false the exit flow will then flip the is pushing to false on the animator and the player will stop pushing hold alt and left click drag to bring out another set variable for this trigger push and then drag out the flow for the input and put a boolean because when we're exiting we want to set this trigger push bool to false so we want to run the exit flow into the same set bool so that when we exit it sets the parameter to false and the animation goes back to the walking and the running we're not pushing anymore hold ctrl and left click drag to make this a group and let's name the group as push animation transitions and i'm going to make the color brown so that it's visually associated with the gate all right let's test it and she's pushing the door it's not perfect but we're just trying to do a little bit of every way to do doors in this video and we'll cover more advanced uh third-person controller stuff in the future okay so let's move on to the large doors uh the jurassic park doors as i initially thought of them so hold shift and left click the camera and the third person character and move them past the gate each door we're doing has its own parent object in the hierarchy so click on the large doors by trigger rotation parent object and this time let's add a state machine instead of a flow machine click new macro call it trigger rotate doors i'm going to add underscore tutorial press shift space to enter full screen we'll have two doors let's start with the left door so name the first state closed l and then right click make another flow state and call it open l then right click and make transitions from close to open and then open to closed press shift space again so uh let's look at the stuff inside this parent object and the left and right door is a child of an empty parent game object that will act as our hinge pivot for the door press e or select the rotate button up here and you can rotate this object the hinge object rotates the child door if you rotated the door directly it'd spin on its center pivot which is not what we want so when our doors are open we want them to both look like this let's start in the open state double-click and delete the enter and exit when rotating from one rotation to another rotation we want to use a quaternion slurp unit on the quaternion slurp you can see we're rotating from one angle to another since we're building our flow graph on this empty parent object we need to make a scene variable for our hinge so call it large hinge l and grab the hinge and put it in the variable value left click drag it out and then drag out the flow because we want to get the local rotation and that is the current rotation of this hinge object so plug that into a because b is going to be what we're rotating to for b we want a quaternion euler with a vector 3 because euler angles are rotations we are familiar with um four-dimensional quaternion numbers are useful but they aren't intuitive to directly work with what euler angle do we want to rotate our door to rotate the hinge object in the scene view and find a good rotation for the doors to swing open and first let's stay at this 90 degrees on the z because that's what the parent object is at i could have fixed that because this is just an empty parent object but i'll keep it for now because it illustrates watching for this kind of thing like something being rotated at 90 degrees so we want to keep that the same let's have the door rotate negative 120 degrees on the y so type negative 120 down in the quaternion euler unit t is a value of 0 to 1 like a percentage for how much to rotate each frame from a to b so let's create a multiply unit we're going to multiply a speed variable to time delta time and i'll explain what that is create an object variable for rotation speed make it of type float and make the value 0.2 left click and drag that in and plug it up to the multiply and we'll multiply this speed to time delta time time to delta time makes our rotations the same no matter if we're at 30 frames per second or 60 frames per second or if we're our frame rate is fluctuating the rotation is smooth and consistent across all of those different frame rates now that this rotation is being calculated let's apply it to the hinge and i don't like crossing my wires so i'm going to get the hinge l seen variable and then i'm going to drag the flow from the quaternion slurp and find a transform set local rotation unit and you know i think i'm going to switch my graph inspector to the right side i like it better up there than on the left we want to set the local rotation on the hinge object so plug the variable into that and then connect things up to the slurp hold ctrl and left click drag to group this up i'll call it open l large door let's copy the whole thing and go to the close state paste the group and let's rename it right away to close l large door so for this we'll keep the constant 90 degrees on the z when the gate is closed y is going to be zero and that's all we need to do for the closed date next let's set up our transitions with the triggers and actually i'm going to rename my triggers to use large door for everything in this section so things are a little bit more consistent i have the open trigger glider out front and the close trigger is past the door so the door closes behind the player let's start with triggering the doors open in the transition to open right-click and create an ontrigger enter unit we want the trigger open game object so create a scene variable for trigger large doors open and it's a game object and i'll drag that object in to the value let's work ahead and do the same for the closing trigger so create a variable for trigger large doors closed i'll make that a game object and drag that object in drag out the trigger large doors open and let's check if it's the player that has entered the trigger so create a game object compare tag unit connect things up and on the clyders game object we want to check if it has the tag player if the object is tagged player let's branch the flow to the opening state drag out the flow from the compare tag unit and create a branch so that we can branch the flow uh to the open door state if an object tagged player has hit this trigger hold ctrl and left click drag and i'll call this group trigger open select this group and ctrl c to copy it because we're going to paste it into the transition to close so in the transition to close we want to use our close trigger past the door so i'll rename the group to trigger closed and let's get our close trigger game object variable for the on enter event when the player hits the close trigger the flow will go to the closed closing state so with all that done save your project and test it and the trigger opens the door and the close trigger closes the door okay for the right side door select the left side and ctrl d to duplicate it and let's start by renaming all the l's to ours so rename the states to open r and closed r go to the open state and look for the ls because those are the things that we're going to need to change so let's rename the group to open our large door and we also see l in these game object variables so go to the scene variable tab and let's make a variable for the right door game object type large hinge r and it's a game object and drag that game object in next the rotations on the quaternion euler are going to be different uh this door is opening to the right so that's going to be plus 120 degrees on the y and actually though we made the hinge right variable we didn't change it and so this time let's change them by using the drop down menu and selecting the right hinge variable go to the close state and change those hinge variables to the right hinge and change the name of the group the closed rotation will actually be the same 0 0 and 90 degrees on the z so save your project and see if it works and you can see the state changes on the graphs okay now we're at the drawbridge great hold shift and select the camera arm and uh the third person character and move them up to the drawbridge area expand the drawbridge by button parent object and take a look at the other game objects in there i'll select the drawbridge hinge and bring up the animator window this time our rotations are simple animations though like the large doors we have a parent object here that is rotating at a pivot that serves as a hinge for the door underneath this time our animations live on an animator on the door hinge and the animation controller has two states draw a bridge up and draw a bridge going down in the animation folder you can find the drawbridge going down animation and you can preview it i have a static animation of the drawbridge up and you can select the transition and you'll see that there is a bool condition a parameter bridge button pressed and if that goes true the drawbridge goes down the other two objects i have are the back of the button and the button itself on the drawbridge main parent object i have a trigger box collider you can see that trigger box collider extending out from the button in the scene view select the drawbridge by button parent object and add a flow machine to it i'll call it drawbridge button underscore tutorial delete the start and update first we're going to use the trigger to see if the player is at the button and we've done this before right click and make an ontrigger stay and then make a game object compare tag so we can check to see if the player is here staying on the trigger let's make a scene variable and call it player is at button and it will be a bool hold alt and left-click drag to set this variable right-click and add an on-trigger exit and drag out the flow another way you could set the variable of course is to just search for it so i'm going to type uh set scene variable and then in the drop down i can find that uh player is at button bool this time let's set this variable as false because the player will have exited the trigger and is no longer at the button hold ctrl and left click drag to group this up as uh i'll call it is player at button question mark so one condition for the player animation to be pressing this button is that the player needs to be there so let's just left click drag out the variable and then create a branch unit so we want the player to press the a button on the controller or the e button on the keyboard to lower the bridge right click type on button input and then run the flow into the flow input of the branch the button we want is uh capital f uh fire one this is the a button on the xbox controller and on the input settings in unity i've made it pressing the e key when the player presses the a button an event will shoot down this flow and the branch will check if the player is at the button let's enable co-routine on the button input it takes time for the finger to reach the button uh in that button press third person character animation we're gonna put a delay before the animation starts so that it doesn't look like the bridge is going down before our character's finger touches the button in bolt it's really easy to have a one second delay on your script drag out the flow and type wait four seconds and let's have the animation delay for just one second drag out the flow and create an animator set bool the bool we want to set is the bridge button pressed parameter that you can see up there in the animator when that's true the transition will go to the drawbridge going down animation so we want to set the bool to true the animator unit is yellow because this particular object has no animator the animator that we are trying to give directions to right now is actually on the drawbridge so make a scene variable for drawbridge hinge make it of type game object and drag in that drawbridge hinge game object i'm going to group up this flow and call it button animates bridge so drag out the flow from the animator set bool and create a renderer set material i thought it'd be cool if the bridge also lit up along with the bridge going down animation make scene variables for both of the bridge lights so that's a variable called bridge lights l and then another variable called bridge lights r and both are game objects and bring in the first light and hook it up to the renderer sometimes i like to stack two units like this if they feel like they're well paired to me so left click drag to select the pair and ctrl d to duplicate it then from the drop down get the variable that's the bridge light right go into the materials folder and grab the glowy green button material onto the material slot of the renderer get material unit then i'm going to tidy things up make sure all my connections are there hold ctrl and left click drag to group this up and let's call it bridge lights and i'll go to the is player at button group and make the color of it the same color as the button and you probably notice but i forgot to plug in the hinge object on that animator it's still yellow so i'm going to get that and plug it in and you can see the yellow color on the node goes away select the third person controller we want a press button animation and i've set it up in the animator with a pressing button bool parameter so let's make a new float right click and make an on button input event again and uh make sure that the name is fire1 and then drag out the flow and let's make a branch again go over to the scene variables because we want to check if the player is at the button left click drag in the player is at button boolean this way the pressing button animation only happens when the player is right in front of the button on the trigger if the player is in the button trigger area and they've pressed fire one we want uh to set a bool on the animator so drag out the flow and get an animator set bool and the bool we want to change is called pressing button and that's going to be true as you can see in the condition for the transition in the inspector now let's make a flow for transitioning back to the regular grounded state right-click and get an update event unit drag out the flow and type animator get current state info then drag out the info and type animator state info is name if the animator is in the state pressing button then let's make a branch that the pressing button parameter is set back to false drag out the bool and get a branch and hook that branch up drag out the true flow and type animator set bool and type in the bool parameter pressing button and that's what we're going to set back to false so basically it resets the pressing button transition so we can do the transition again [Music] so as soon as the pressing button animation is happening we're setting this pressing button parameter to false so it can be true again i'll tidy things up and control left click drag to group it and i'll call it press button animation i'll also set the color of this group as the same color as the button material and then i'm going to save it and let's test it okay the button is pressed we see the press button animation the lights light up and the drawbridge goes down and what is wrong with my knights uh i think that that happened because i've been organizing things uh a little bit so um i will make sure to fix that just like before let's grab the third person controller and the camera arm and move them forward towards the knights but be careful to not be on the trigger in front of them the knights are underneath this animations by event parent object select one of the knights and you can see in the animator window that i have the animation states and the animation set up this is similar to the drawbridge only this time we're going to use custom events to change the animation states on the nights select the past night event trigger object and add a flow machine make a new macro and call it trigger custom event underscore tutorial i'll delete our default events and right click to create an ontriggerenter same as before drag out the flow and get a gameobject compare tag connect it up and for the tag type player with a capital p drag out the flow from the game object compare tag and type in custom event trigger custom event let's call this custom event pass night and that's all we need for the trigger next let's work on the night prefab so go to the prefab folder and find the prefab for the knights and add component add a state machine click new to save a new macro and let's call it night states let's call the first date night closed pose and then make another flow state and call it knight open pose and then select the close pose and right click to make a transition from the closed to the open let's start with the closed pose so i'll delete on exit and keep the on enter in the update on enter i'll drag out the flow and add an animator play the animation we'll play is the night closed animation and by night closed i mean the knights are crossing their swords drag out the update and type animator get current animator state info drag out the info and go into the animator state info and let's get the normalized time i want to pause the animation at a certain time pull out the float value and type greater because we want to stop this animation if it gets to be greater than a point in the animation which is .26 drag out the flow from the animator get current state info and create a branch unit if the animation uh is past this 0.26 point we want to set the speed of the animator to zero so we're basically pausing it hold ctrl and left click drag and let's group this up and call it freeze night animation and i'll fix that left knight in a moment there's a really easy fix for that kind of thing let's copy everything in this closed state and then paste it in the open state delete our starting stuff in the open and then paste what we're bringing over from the close state so what do we want to do on the enter of this night open state well uh we definitely don't want the night closed animation so i'm going to delete that this standing open pose is not really an animation it's just one pose so we probably don't need this freeze night animation group i'll just put 9.99 in the greater than pull out the on enter state event flow and get an animator set speed unit because we want to set the animation speed back to one drag out the animator set speed flow and type animator cross fade in fixed time and then under the name type night standing open because that is the animator state that we want to fade into and then make the transition duration three seconds the reason i'm doing this is to show you that we don't need transitions in the animator we can actually uh script those so the giant knights will take a good long three seconds to crossfade the pose where their swords are crossed to the open post for the transition from the closed pose to the open pose let's make our past night event trigger object a scene variable make it of type game object and drag the past night event trigger object into the value drag out the flow and let's get a custom event unit and the event that we want is the past night event custom events are great for passing arguments like healing or damage we're not doing that now so we're not using the full potential of custom events but i still wanted to include it so that you're familiar with the idea when we eventually do those things to fix the scene pose of a character all you need to do is uh play the game pause it copy your character in the pose that you want and then paste it after you've stopped the play mode and then delete the old one that has the pose that you don't want okay let's save the game and test to see if the custom event works for the knights and it does great grab the character in camera arm and let's move up to the portal expand the portals by trigger object and inside there you'll see that the portal on the left is called portal 2 scene load so on that portal click add component and add a flow machine click new macro and let's call this trigger portal load scene and i'll add underscore tutorial for myself right click and make an ontrigger enter and then drag out the flow and get a gameobject compare tag and for the tag type player we've done this before from the bool let's make a branch and connect that branch up and if true so drag out true we want to get a scene manager load scene and the scene that i want to load is uh the scene from the previous video the ai behavior uh number one bolt and then i don't think i'm going to group that up oh and before i test it make sure to connect up the collider to the gameobject compare tag unit okay let's test it and this is the scene from the ai behavior and bolt tutorial i've included this in the package as well on the portal to beginning object let's make a flow machine make it a new macro and call it trigger portal to position underscore tutorial and if you want you can copy over the enter gameobject compare tag branch flow from the portal to scene load or we can just remake it real quick on trigger enter game object compare tag connect that up make the tag the player bring out a branch from the bool and then on true what we want is a transform set position and i have a start location game object at the top of the hierarchy window make a scene variable and call it start location and drag in that start location game object drag out a get variable of the start location and we're going to need a transform get position plug that position into the transform set position now we aren't setting the position of the portal that would be the self we're actually setting the position of the third person controller and as we've all learned from the movie the fly we want to be really careful about what things go through portals so get a game object find game object with tag unit so when the player hits the trigger on this portal the flow will find a game object that is tagged player and then the transform set position will send the player to the start location position let's test it and that works we're at the beginning and then uh for the portal on the far right i have a surprise and this portal does nothing at all you fall to the abyss congratulations and well done for making it through each door example here i'm jim homek on discord and all the other things and i hope this helped level up what you can do with bolt
Info
Channel: TriggerHappy
Views: 3,585
Rating: undefined out of 5
Keywords:
Id: O2s9EwzeqTM
Channel Id: undefined
Length: 35min 53sec (2153 seconds)
Published: Thu Oct 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.