There's a wide misconception that the speed, how fast something is travelling is the same as its velocity.
But it's not.
Velocity is in fact a vector value that not only represents the speed of an object, but also its direction
of travel.
Velocity is a quantity that expresses the rate of change in the position of an object over time.
Velocity is measured by distance, traveled divided by the time of travel.
Mathematically, it's represented as V equals delta d over delta t.
The delta or triangle symbol means change in.
For example, if a ball starts at zero and after one second is at ten, its velocity would be ten meters
per second, and in this case, in a horizontal direction.
If the ball starts at one and after 2 seconds is at 15, then the distance it has travelled is 14 meters
in 2 seconds or seven meters per second.
Also travelling in a horizontal direction.
If you've already been moving objects about in a game world, you've been working with velocity.
In fact, to move something, we must always adjust one or more of its x, y or z values in unity when
we use translate.
This is in fact what we are doing all at once, adding a vector three to the position of the object.
To get a really good feel of how this works.
We're going to program some stuff into unity so that you can experience it.
So attached to this lecture is a unity package, and if you bring that into a new project, you'll see
if you open up the tanks.
Seen that there's two little tanks in here.
Now, we're going to use this scene a little bit later.
But for now, to just have a look at velocity, we're going to create a new scene.
So once you've got that, let's go file new scene and it'll be a 3D scene and into here, we're going
to add in a bullet.
So if you go into prefabs, you're going to find that there's a shell in there.
Just drag that into the scene.
Now you should be able to see it over here in the game view.
If you can't see it in the game view, just select your main camera and go game object, align with
view and then you will be able to see what this scene view is using.
And then you can adjust the camera as you like.
If you want to see what my camera settings are, you can pick them up from this transform here.
Okay, so if we grab hold of that shell, we can actually probably just move it closer to the camera
and just across a little bit and up so that we can actually see it in there.
Now in the scene view, I'm also looking down the positive Z axis.
So to do that you would click on this axis symbol at the top here and click on the Blue Z.
So that's going into the screen.
Okay, so this is your bullet and if we just double click on it, we can get in a little bit closer
to it.
We want to rotate the bullet so that it's actually facing across the screen like that.
Now, if you're not familiar with the settings for it and sometimes orientation in 3D can get quite
confusing.
What I usually do is I just select the shell.
Now the up axis in this case is the y axis.
Now we want to rotate around the Y, so you grab hold of this y axis over in the inspector and you can
just start to sort of turn it like that and you'll see that you're turning either in the correct way
or the wrong way.
Now, for me, I'm turning in apparently the wrong way here, but you'll see over in the camera view
and I am facing across the screen that way.
Now, if we turn 90 degrees, we're going to be at 90 degrees like that.
Let me now just turn this around.
So we're looking at it like that.
Okay.
So another thing that we need to be aware of when we start to move objects around is their orientation.
This bullet, when you click on it and hit the W key, you can see the axis system, the blue axis,
the Z axis is otherwise known as its forward axis, and that's what you want to push it along its forward
axis.
This is in the bullet's local space.
Okay, so the Z axis is going forward across the screen here, but you'll see that we set our Z axis
to go into the screen.
But this is the local coordinates to the bullet.
If you want to see its global coordinates, then you would come at the top where this little cube is
and click on Global.
And now you can see that globally the positive Z axis is going that way and this would be the Y axis
here.
But whenever we use a transform forward or transform translate along the Z axis, then we're actually
moving an object along its own Z axis.
So in this case, we really want to be on local and note the local coordinates system is okay.
So now it's time for a bit of a challenge.
But just before we do, what I'll get you to do is get that shell prefab that you've used now it's already
got some scripts and things on it and we're actually going to remove those.
So in the inspector, just get your destroy shell and remove that component and these rigid bodies,
let's remove that.
The capsule collider will remove that.
And there's another shell script that will also remove from there.
Now the current version of unity will say that it's removed even though you'll still see it.
So you could, if you needed to go, control Z to get those back.
Okay, so let's look at the challenge.
I'd like you to write a script called Move Shell and attach that to the bullet so it moves it across
the screen at one meter per second.
Okay.
So pause the video now, give it a go.
And when we come back, I'll step through the solution with you.
Okay.
Welcome back.
How did you go?
Well, first of all, let's create our script.
So we create a C-sharp script and we'll call it Move Shell.
Now let's open that up.
And in here, we're basically going to push the shell forward with a transform translate.
So we go this dot transform, that translate and we move it along its z axis.
So zero zero and in he will put time dot delta time.
Now this is going to be multiplied by a speed.
Now we wanted one meter per second, so fair enough, we would multiply it by one, which you actually
don't need to do, but let's make a value for speed so you could speed it up if you wanted.
Then up the top we will put in here a float for speed and set that to equal 1.0 f.
Okay, so that's pretty much it.
Save that.
Let's go back into unity and we're going to grab hold of that shell and drag and drop this move shell
onto it.
And then we can press play and have a look at the behavior of the shell.
Okay.
So you've basically set the velocity of this shell to one meter per second for its movement along the
Z axis.
Now, what if you wanted to move at half the speed on the diagonal up this way, then what?
You're moving across the screen there.
So this is your next challenge.
See if you can modify that translate in order to get a diagonal movement at half the speed up to whatever
it is across the bottom.
Pause the video now.
Give that a go.
When we come back, I'll go through it with you.
Okay.
Welcome back.
How did you go with that?
Well, to get it to go up the screen in that particular direction, we're going to modify the Y axis
because that's the up one.
And this is this value here.
So basically, it would be the same as we've got for that.
But we want to multiply that by 0.5 F, which is going to give it half of whatever this speed is so
we can save that, go back into unity and press play on that to now move it and you can see it going
up there on the diagonal.
So this code demonstrates very clearly the difference between speed and velocity.
Speed is just a value or a magnitude.
In this case, we've got it set to one, but velocity is a vector.
This is a velocity.
Velocity has a magnitude and also a direction associated with it, a very important concept to get a
hold of.
When we come back in the next lecture, we're going to look a little further at this velocity and how
we can predict where an object will be in the future, which is often used in AI systems where a character
can predict where the player is going to be based on the player's current speed and direction of travel.