 Hey there class welcome back to game Dev Academy. I'm Shane make sure you sign the register by leaving a comment Way down below just like these amazing students did in the last class This class is going to focus on making the game a bit more predictable So at the end of the last step I showed you that the kind of bounce was a little bit out of our control We can get some weird acute angles that we don't want and this one we're going to get what that angle is from the ball We're going to change it to be an angle that we want it to be and then put it back on to the ball using blueprints again So if you're feeling ready, let's get stuck in Right here we go then this step is going to be all about controlling the way this ball is going to move So that it behaves predictably and in a way that's going to be more fun So I'm going to keep it at 45 degree sort of increments of now at 90 degree increments that that's going to run diagonal So what we need to do is get into the blueprint for the ball and set this up here we go So it's going to our blueprints holder Here's my ball and I'm going to hop straight into the event graph when it opens up. Here it is So this is my event graph So what I'm going to do is create a new custom event and that's going to be the thing that will happen That's going to make the ball do what we want it to do So I'll right-click and I'm just going to start typing custom event and Then the first thing I'll need to do is give this a name and I'm going to call this velocity since we're also going to use this to keep the the speed the same and Angle is what I'm going to call it there might be better names for it But I know what this means so that means it works for me. I'm going to call this every tick So what I'm going to do we've got something happening On event begin play But up here. I'm just going to have Event tick so this will happen every CPU cycle and it's going to run Velocity and angle There we go. So that's set up. So now what we need to do is build this script It's going to be slightly more complex than what I've done so far the difficulty curve kind of goes like like this straight up So stick with me and I'll try and explain what I'm doing as I'm doing it So the first thing we need to do is get the velocity of the ball So the ball already moving or we need to get that information before we can manipulate it So I'm going to do get velocity and it's this one here. It'll say target is active Which is what we want because that's our ball So we'll do that first of all and then what we want to do is clamp that so out of the return value I'm going to type clamp Vector size and what this will allow us to do is to clamp that velocity the actual speed To a constant value so you could clamp it between like a high and low value But we want to keep it exactly the same speed and we'll use this node to do that So we'll need a new variable to represent the speed of the ball So here I'm just going to click on plus. I'm going to call it ball speed I want this to be a float so I'm just going to select this I'm going to change it from Boolean to float remembering that float is a number that can have decimal places And then we'll compile that so that we can set the default value to for this to 1,000 Like that and that means that the ball will start out at that velocity at that speed So what we'll do now is we'll get this ball speed and bring that in and get and We're going to plug this into the min and the max values of the clamp vector size So what that will do is it's going to take whatever its velocity is and make sure that it can only be 1,000 and what it also means is that when we come back into this variable at any point We if we think the ball is running too fast We can change it if it's running too slow We can change it or if we want to speed up this like the speed of the ball throughout the game to make it More complicated then we can control that here as well, which makes it nice and easy So that will basically control the speed what we now need to do is get the angle sorted out And we're going to take this vector size, so we've got the speed so far, but we haven't really done the angle So let's drag out of return value and then we'll create something called a rotate Vector here it is And we can use this to take the vector which is the direction of balls moving it I rotate it to be whichever direction we want it to be in so to do that We're going to drag out of here and get something called a make rotator This little chap here And we're going to set the z axis which is the yaw to 45 and it's this change here That's going to make sure that we're always operating in diagonals, which is what we want so out of the return value of this We're going to create a make rot from x and Then what we need to do this return value has got x y and z properties But we need to control those individually and in order to do that We can right-click on it and we can split the struct pin Which then will give us return values for each of the three axes we can rotate them So what we'll do now is get the return value from the z axis And we need to divide that so this will be returning a number which will be a float And we're going to divide that by 90 so we can use that to give us one of four angles You'll see how it works in a minute So you can get a divide by either putting in a forward slash or you can type divide and I'm going to do a float divider by float and I want to divide this number by 90 And that will mean that whatever the angle is it's going to When we round this it can only be one of four angles So the problem with this at the moment though is that we can't Work with it yet until we round it up or down to the nearest integer So we're going to drag out of here take the result and we're going to do a round And that will round the number to the nearest integer And you can see the color coming out of here has changed now because it will only return an integer Which is what we want. So if we take a number like let's say it's going an angle of a hundred and sixty and divide that by 90 Round it and then what we'll do next in a minute is multiply it by 90 That's actually going to round it back up to 180 degrees So it only gives us the angles that we want So we're going to take the return value of them and we're going to multiply so again You can either type multiply to get this or you can just put in an asterisk And we're going to do an integer and we're going to times it by integer in this case And we're going to times this by 90 or multiply this by 90 There we go lovely stuff so far So what we've done then is taken what is in any number between sort of zero to three sixty or zero to minus three sixty Done some comaths with it and we've spat out an angle that can only be zero ninety one eighty two seventy three sixty or minus ninety one eighty two seventy and that's going to be what keeps control of this ball for us So we'll get a mate rotator And then the result of this it needs to be plugged back into Z So in doing that, you'll see this little conversion node comes up And it takes the integer and changes it back to a float now What I could have done is I could have done a an integer times float here And then the output would have been a float But I wanted to show you what happens if you need to convert one type of number to another Unreal Engine works that out for you, which is a nice handy little trick. So with this value We're going to get the return value and we're going to connect it to a get rotation X vector And this is what we can use then to control the direction of the ball again And then we need to unrotate this so out of here. I'm going to go unrotate vector Lovely and then we need to make sure that we get it at 45 degrees again So from here this one here the mate rotator We could make a new one over here if we wanted to but what I'm going to do is just drag the result here Into there Because these are both doing the same thing so we're rotating it here, and then we're unrotating it again here By 45 degrees so we need to multiply this by a vector length now, so let's create a vector length length there it is vector length And we can see that the result of this is a float so out of here We need to do a multiply and we're going to do a vector multiplied by float So let's plug that in there and Then we need to figure out what to do with the result but before we can do that We need to get something coming into the vector length, which is going to be the speed of the ball so Here's the ball speed So the clamp vector here is what needs to come out of here, so I'm going to bring this over here And I'm so lost right now and then plug that in there So that's getting the speed that can be multiplied by the angle and then we're going to use this Output here, so we've got Angle up here speed here, and then we're going to put that back onto the ball So we started way back at the beginning by getting what was happening with the ball and now now don't know this Modification of it. We're going to put it back onto the ball. So out of here. We're going to create a set physics linear velocity of the ball There we go And what it does is it gets the ball for us So the static mesh here so that it knows that's the target and the new velocity is the result of this math That we've been doing Okay, so now we need to trigger all this And so to do that we've got an execution pin all the way over here And so I'm just going to bring this over and this just needs to be connected in here so Every tick what's going to happen is this is going to be triggered which by the time I've gone through it all We are getting the velocity Modifying it changing the angle putting it all back together and that's our new velocity So what we need to do now is compile and save this and then we'll give it a test and see whether or not it allows Us to get any different angles and what I'm going to do here is just a little trick So as well as doing this I'm just going to get to print the the angle of the ball So that I know what's happening. So what I'm going to do is after it's done this I'm going to do a print string and this will print the angle for me as some text in the top left-hand corner of the screen So as I'm only really interested in the angle I'm going to get out of here the make rotator We'll plug that into the in-string again. It will convert it because It's coming out as one type and we want it as a text string so we can see that modification happening So I'll compile that again save and then let's test Balls not launching Okay, then so it's clear that the testing as highlighted a problem And I know what it is. I've done something very silly So the way we've currently got it set up. We're actually using a set physics linear velocity here And that's what I should have used in the first place I should have got the physics linear velocity, but instead I use this nonsense node here to get velocity So we need to swap that out. So instead of get velocity. We're going to use get linear Physics velocity for the ball. That's the one that we should have used. I apologize for showing you the wrong thing So we'll need to swap this out. So let's get rid of that And we will drop this bad boy in its place and then what we need to do is This execution pin here needs to go back over here and the first thing we're going to do is get the full up the velocity and then the return value is going to go into there and Then we can take this pin here and Plug it into there. Ah, or we could if I was a better aim There we go. So what we do now it'll make more sense is we Are running this custom event? Let's get the current velocity of the ball the physics linear velocity as we should have done in the first place Do all the same things to it and Then set it back onto the ball and we'll do the print string again to see whether or not it's working this time So it's compile and save cross your fingers Hey, there we go. And then you should be able to see that on why and The the angle is changing. So there we've got minus 90 There's 90 as it comes back down. There's 180 and that's what that maths has done for us It's making sure that the ball will only ever move at one of those four predetermined angles as we've set them and That will ensure that the game remains predictable But the ball is not going to spend too long at one end of the play field and that's going to keep the pace of the game up You can obviously do your own little trickery with the maths to get The ball moving in different directions if that's what you want or to kind of correct it if it's moving at any unwanted angles But as I'm doing this as a making your first game in Unreal Engine tutorial I don't really want to make it any more complicated than that because as you can see I've done this before and I still cocked it So there we go. What we'll do now then is We'll remove this print string because it's served as purpose So in the next step what we'll be doing is just kind of tidying up the work that we've done in this step So we're going to use something called reboot notes to try and tidy up some of this spaghetti looking stuff here And we're also going to comment any of the bits that we haven't done so far So these areas could do with commenting just to keep on top of things. So it doesn't get too confusing Plus, it'll be a nice easy step after this more complicated one. I think we're already for a little bit of a breather That was hard work. So I will see you in the next step. I believe that quality education should be available to everybody and for that reason all of the classes at Game Dev Academy are Completely free and we're supported by our very generous school governors over at patreon If you'd like to become a Game Dev Academy governor and support our work as well as helping us to steer the channel in the Right direction then use the link in the description to be taken to the patreon page