‫Hello and welcome.
‫In this lecture, we're going to be looking at how we can set our moving platform using a vector velocity
‫in its member variables.
‫And we're going to look at how we can make its movement frame rate independent using something called
‫Delta time.
‫Okay, let's dive in.
‫So up until this point, we have been just updating a single component of our vector at a time.
‫Now, what if I told you that we don't have to do it this way?
‫What if we can just update the whole vector by a certain amount in one go?
‫Well, we can vectors can be added to other vectors if we've seen in the previous section.
‫So what we could do is go over to our moving platforms H and create something which is our moving platform
‫velocity vector that tells us both the direction and the speed at which we want to go.
‫So the way we do this is create a rather familiar by now U property and we're going to make it edit
‫anywhere so it's visible in the inspector and then it's going to be a type F vector as we've done before,
‫and we'll call it the platform velocity and we can give it something for now.
‫So we're going to do an F vector constructor and then we'll just give it a say 100 in the X component
‫and zero and zero in the others and then a semicolon to finish it off.
‫And I want to show you just one more trick here is that along with edit, anywhere we can put another
‫comma separated argument to this U property thing, which looks a little bit like a function.
‫Actually, you can put one that is called category with a capital C and then you put the equal sign.
‫And I usually leave a space between these equal signs category equals.
‫And then you can give a category that you want to show up.
‫Down here you can see there are categories for transform rendering replication.
‫These are the kinds of categories we mean I'm going to call mine moving platform so that we have a kind
‫of moving platform category to put these variables into.
‫And note that I use a double quotes around that category.
‫That's the correct syntax.
‫You do need to include those.
‫And then what we can do is we can make use of this new member variable over in the C++.
‫So instead of setting the X component of current location, let me entirely remove that line and rewrite
‫it as the current location is equal to the current location plus platform velocity like.
‫So put semicolon on the end of the line and let's go over into Unreal and hit compile and see how this
‫goes.
‫So that's compiled successfully.
‫And you can see I now have this moving platform section on my moving platform with a platform velocity
‫and its x component is set to 100.
‫What happens if I go and hit play?
‫Well, the platform speeds off rapidly into the distance because I'm adding 100 units every single frame.
‫That is a lot.
‫But I do want it to move at a velocity of around 100 centimeters per second.
‫That's just one meter per second.
‫It's not very fast.
‫So why is it going so fast?
‫Well, because of my frame rate.
‫Well, if I was on a slower system, it would be moving slower.
‫And if I was on a faster system, it would be moving faster.
‫That doesn't seem to make sense.
‫So let's talk about using Delta time.
‫What is Delta time?
‫Well, using Delta time in Unreal can tell us how long each frame took to execute.
‫Where's Delta time?
‫Well, if you go to Visual Studio code, you can see Delta time is a variable mentioned here at the
‫top of tick.
‫This is actually a input parameter.
‫It's the input to our function.
‫So we can use it here essentially like we'd use a local variable, we can use it within the body of
‫the tick function.
‫So it tells us how long a frame took to execute.
‫But why is that important?
‫Because when we multiply something by delta time, it essentially makes our game frame rate independent,
‫which basically means that the game behaves the same on a fast or a slow computer.
‫So let's take a look at an illustrative example if I want to each second move something by one unit
‫to the left.
‫Here's how it would look on different machines.
‫I've got a slow computer.
‫It can only do ten frames per second and a fast computer which can do 100 frames per second.
‫Now the duration of those frames is different.
‫So on the slow computer, the duration of the frame is 0.1 of a second.
‫If I can only fit ten of them into a second and on a fast computer it would be 0.01 of a second, so
‫much less because it can execute things faster.
‫It doesn't take as long to compute a frame.
‫So if I multiply by the duration of the frame, I multiply my one unit by the duration of the frame.
‫Then you're going to see that in 1/2.
‫So where ten frames have been executed, I get one unit of movement.
‫The same is true.
‫If I've done 100 frames of execution, I still get one unit of movement.
‫So I'd like to challenge you to multiply by Delta time.
‫So what we're going to do is remember that we can multiply vectors by a floating point value to scale
‫their magnitude.
‫And the way we can do this in C++ is using the star operator.
‫That is how we do multiplication.
‫There's no multiplication sign operator.
‫It would be confused with an X, so we use the star operator to denote multiplication, and I'd like
‫you to compile and test out your results.
‫So multiply by delta time, multiply the speed by delta time and see what happens, plus video and have
‫a go.
‫Okay.
‫Welcome back.
‫So it's the platform velocity that we wanting to multiply here.
‫Now we could pull this out into a separate variable, but in actual fact, we can just put a multiplied
‫by delta time at the end of that line, obviously before the semicolon, because it's before the end
‫of the statement.
‫And what's interesting here is that there's a bit of a precedence of operators going on here in that
‫the multiplication between the platform velocity and Delta time is going to happen before the addition
‫to the current location.
‫Now this follows normal mathematical rules for a full list of those rules.
‫I'm not going to go into the details of it right here, but you can have a look at the page on order
‫of operations on Wikipedia.
‫There's lots of different rules in different languages, whether it's body mass, bid mass, ped mass,
‫bed mass.
‫There's loads of different acronyms.
‫So whichever one you grew up using, that's the one I would recommend you use.
‫For me, it was body mass, but in other places it's different.
‫So it's worth noting that we can have just write it like this, but if we want it to make sure that
‫something happens in a different order to that, then you can use parentheses to indicate this.
‫So I could, just to make things clearer, I'm going to put parentheses around this multiplication just
‫to show you that that's what happens.
‫First the multiplication and then the addition to the current location.
‫So let's go over into Unreal and test this out.
‫We're going to hit, compile and when that's done, let's hit play.
‫And now we see the platforms moving at a much more normal speed because actually it's moving at one
‫meter per second, which is okay for a moving platform.
‫So you can see that this has helped us both reduce the speed, but also now this is a frame rate, independent
‫speed.
‫If my computer was chugging along because something else going on in the background, this would still
‫look the same and play the same in game.
‫Great stuff.
‫I will see you in the next lecture.