‫Hello and welcome back.
‫Now in this video, we're going to start talking about how we can move our tank.
‫So to move the tank, we're going to have to add some sort of offset and an unreal engine.
‫When it comes to adding an offset, we need to make the decision whether to make the offset in local
‫space versus world space.
‫So let's discuss the difference between these two things.
‫Now, here in the editor, I have the -- tank selected, and as soon as I have this selected, you'll
‫notice that there is a gizmo.
‫That's what this thing is called, a gizmo.
‫It has three arrows and they're each a different color.
‫Now, if you haven't noticed, at the bottom left of the screen is another smaller gizmo.
‫And this sort of looks similar to the gizmo on the actor that we're selecting.
‫So if I select any actor, we always get this gizmo.
‫And of course, the gizmo changes when we go from translate to rotate to scale mode.
‫So as long as we're in translate, which we can access from this leftmost icon at the top right of the
‫viewport or with the W key, we have the ability to move things or translate them by dragging on one
‫of the arrows on the gizmo.
‫And the colors correspond to the axes red as the x axis green is the y axis and blue is the z axis.
‫Now, if we rotate our view and look in a different direction, we'll see that that gizmo still points
‫in the same original direction.
‫And as we're rotating around, the gizmo at the bottom left of the viewport is rotating because the
‫world direction does not change.
‫So far, so good.
‫Now what we're looking at is world space directions, but for each actor in the world, there is a local
‫direction.
‫And we can see that by clicking on this icon that looks like a world and the pop up says cycles, the
‫transform gizmo coordinates systems between world and local space.
‫When we see a world here, that means we're looking at world space, but if we click on it, it now
‫turns into a cube and we're now looking at local space for the given actor that we have selected.
‫The gizmo at the bottom left of the viewport still shows us world space.
‫But if I take this actor and I switch to rotation mode and rotate it, then when I go back to translation
‫mode, you'll see that the gizmo is now pointing in a different direction.
‫The actor itself has its own local x axis.
‫We can think of the x axis for the actor as its forward vector.
‫Now notice that at the bottom left of the screen that gizmo is still pointing in the same direction.
‫But this actor here, the pawn tank, has its own local X direction and it's pointing in a different
‫direction.
‫Now, if I click on the cube here and turn it back into a world now, the gizmo for the tank is matched
‫up and aligned with the world axis direction so we can switch between world and local space for any
‫given actor to see its local direction versus its world direction.
‫So we've seen that for any actor that we select in our world, it has its own local direction.
‫And this may or may not be different than the world direction as indicated in this image here.
‫So this is very important when it comes to moving objects in Unreal Engine.
‫There are functions for moving objects and there are a different set of functions for moving in world
‫space versus local space.
‫Let's say our tank is facing this way and its x axis is pointing slightly to the left, as indicated
‫by the arrow that I've drawn here on the tank.
‫Now we can see, based on this gizmo to the bottom left, that the world x axis direction is pointing
‫to the right.
‫So the tank is not aligned to the world space direction.
‫Now, say we were to move this tank in world space in the positive X direction.
‫Then even though the tank is pointing to the left, it's still going to move in that world x direction
‫and it'll look like it's moving backwards.
‫So that's not what we want.
‫Now, on the other hand, let's say we use a function that's designed to move the tank in its own local
‫space.
‫That means if we move the tank in its own local X direction, it's going to move forward in the direction
‫you would expect.
‫And that is the behavior that we want.
‫Now in Unreal Engine.
‫The function that can do this for us is called Add Actor Local Offset.
‫This function moves the actor in a local direction.
‫Now it applies an offset, which is a single movement.
‫Essentially, we tell it which direction to move and how far, and it moves that object once, which
‫means we're going to call this function every frame that we want to move the tank.
‫So we're going to be moving it in small increments since calling a function every frame.
‫Means we're calling it a lot now for our tank its own local X-axis is its forward direction.
‫So this is the axis we're going to use when we add a local offset.
‫Now here's the unreal engine documentation for ADD actor local offset.
‫You see that it's a function that belongs to the actor class.
‫That's why it's called add actor local offset now because it exists on the actor class.
‫Pawns will inherit this function so we can use it.
‫Now here's the syntax.
‫It has four input parameters.
‫The first is an F vector called delta location.
‫So we're going to need to create an F vector that we can pass in to this add actor local offset function.
‫So we know about this add actor local offset.
‫And I'd like to experiment with this just to see how it works so we can do this in the tick function.
‫Now we are not overwriting the tic function here in the tank class, but we do have it in the base pong
‫class.
‫If we go to base pond CP here we have the tic function and we're not doing anything here and this is
‫perfect to do some experimentation.
‫So what I'm going to do is type, add, actor, local offset just like that.
‫And I'm going to right click and select go to definition.
‫Now here in the actor CP file, we have the definition for ADD Actor Local Offset.
‫Now notice something interesting.
‫It's checking if root component is not null and if it's not null, then it calls this function add local
‫offset.
‫So really what we're doing is adding an offset to the root component.
‫And since all other components are attached to the root component, they'll move along with it.
‫So it's important to note that when we're adding a local offset, we're moving the root component and
‫everything else just moves with it.
‫Now, if we hover over add local offset, we'll see that all four of the input parameters are listed
‫here.
‫And aside from the first one, which is an F vector, the others have default values.
‫Notice bool B sweep it says equals false.
‫The third and fourth input parameters also have these default values.
‫This means that if we call this function and do not pass in inputs for the last three input parameters,
‫then they'll be set to these values by default.
‫Now I'm going to close actor CP and here in the base pond tick function I'd like to call add actor local
‫offset each frame just to see how it works.
‫Now we know that the first input parameter is an F vector.
‫It's called delta location.
‫This is the amount of movement we would like to add each time we call this function.
‫So I'm going to create an F vector called delta location.
‫So F vector delta location.
‫Now I'd like to zero out this vector.
‫In other words, make sure that it's initialized with all zeros.
‫Now I want a zero vector I can pass in zero f, an x, y and z will all get zero f.
‫So this will serve my purposes just fine.
‫Now I'm going to take Delta Location X and I'm going to change that to two point F.
‫That way we have a vector with Y and Z set to zero, an x set to two.
‫So I'm going to go ahead and pass Delta location into add actor local offset as a sort of experiment.
‫Now I can get away with calling add actor local offset and just passing in the first input parameter
‫as the other three are optional.
‫So I'm going to compile this and see what happens.
‫Now if we go ahead and hit play, notice, a couple of things.
‫First of all, look, the tower is moving, too.
‫That's because we put this in the tick function for base pawn and both of these bonds inherit from base
‫pawn.
‫So in fact, all of these ponds were moving.
‫And the interesting thing is that they're all moving in their own x axis directions.
‫We can see everything move at once from this point of view.
‫If we click the dropdown next to play and select simulate, this allows us to play the game without
‫possessing any ponds.
‫So if I click Simulate, check this out.
‫Every pond is moving in its own x axis direction.
‫That is actually pretty weird and pretty cool.
‫So this shows us how ad actor local offset works.
‫It moves things in their local directions.
‫Now we can get out of simulate mode by clicking the dropdown and selecting selected viewport and this
‫will allow us to play in the regular play mode.
‫All right, I'm going to stop this and we can go back into base -- and we can remove this experimental
‫code.
‫I'm going to go ahead and just delete it out of the tick function so we now know how add actor local
‫offset works.
‫So let's go back to Tank CP and here we have the MOVE function.
‫We know that this function is being called every single frame and we know that value is equal to one.
‫If we're pressing the W key and it's negative one, if we're pressing the key and it's zero if we're
‫pressing both or neither.
‫So for your challenge, you're going to move the tank using add actor local offset.
‫So in the MOV function you're going to create an F vector called delta location.
‫Now make sure to zero out X, Y and z.
‫You can do this in any number of ways, such as using one of the various f vector constructors.
‫Now once you've zeroed out all the components, then set its x to the value of value that float input
‫parameter in the MOV function.
‫Once you've done this, go ahead and call add actor local offset passing in delta location.
‫Pause the video and do this now.
‫Okay.
‫So I'm back here in the tank CP file.
‫Now, I no longer need this EWI log, so I'm going to take that out and I'm going to create an F vector
‫called Delta location.
‫Now I can use one of the F vector constructors.
‫I've already shown you how to create a new vector with all zeros.
‫I'm going to show you another way, which is to initialize this with f vector, zero vector.
‫And this is exactly what it sounds like, a vector with zero for each component.
‫This is just to show you an alternative way.
‫So we have a zero vector.
‫Now I'm going to take delta location X and set that equal to value.
‫And finally I can call add actor local offset passing in delta location.
‫So let's compile this and back here in the editor, I'm going to take my tank and I'm just going to
‫orient it so that it's facing the world X direction and then I can hit play and now clicking in the
‫viewport.
‫If I press the W key, I'm moving forward and if I press the key, I'm moving back.
‫If I press both, they cancel each other out and I'm not moving at all.
‫And of course, if I don't press anything, I don't move either.
‫So we now have movement for our tank, but it's a little slow, so we're going to have to do some adjusting.
‫And we're also going to talk about fine tuning this movement mechanic with our tank.
‫So in summary, we learned about ADD Actor, local offset and the difference between world space and
‫local space.
‫In the next video, we're going to continue perfecting this movement mechanic with our tank.
‫I'll see you soon.