Check out a free preview of the full Unity for Web Developers course:
The "Camera Movement Solution" Lesson is part of the full, Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:

Nick walks through the solution to the camera movement exercise.

Get Unlimited Access Now

Transcript from the "Camera Movement Solution" Lesson

[00:00:00]
>> Here is the solution that I came up with. This is a slightly more ambiguous task, so there may have been other solutions that are just as valid. But if we take a vector, which I'm calling target here. And we take the target position of the x and z, and add that to the offset, and then leave the y the same, we'll have the position of the target but away from it.

[00:00:39] So the target in this case would be the action on the board, right? And then we can linearly interpolate from the current position to the target. And there's a couple of options for what the target could be. I mean, it could potentially be the player, it could be the opponent, it could be the collectible.

[00:01:05] I think, in this case, because the player and it's a single player game, we should make the target the player. So it's going to frame the action in that way. So let's code this up. And to do that, we'll create a new script in Unity, and we'll call it CameraMovement.

[00:01:33] And we'll attach that to the camera. So I'll drag that over to the main camera. And now, we have a script called CameraMovement, we'll save that. And now, I want to open that script, And let's type that up. So we don't need these other namespaces, we just need the Unity engine namespace.

[00:02:00] And then inside of Start, Well, actually, first, we need some variables. Just getting ahead of myself. So first, we need a transform that we're gonna follow. So SerializedField, Transform, and this will be our target. Then we'll say SerializedField Vector3, and that's going to be the boardCenter. And then we'll have another Vector3, That's going to be the offset, okay?

[00:02:45] So for the Start, going to Calculate what that offset should be. And we'll say transform.position, and we're going to subtract the position of the camera from the center of the board. And now, in update, we need to actually move the camera around. So to do that, we'll set the movement target to the target's position plus an X and Z offset.

[00:03:34] So that will be a vector3. And we'll just call that target without the m. And this will be a new Vector3. And for the x component, we need the member variable target position.x + the offset.x. The y value can just be the transform.position.y, we don't really need to change that.

[00:04:12] And then the z value, same thing as what we did here. So I'll actually just copy and paste that. But instead of x, want it to be z, and we'll end the line with a semicolon. So now we need to linearly interpolate or use a ulurp the position from the current position to the target.

[00:04:44] So just like how the opponent moves around, we wanna move some value. In this case, the cameras transform position from Its current position, so transform.position. We wanna move it toward that calculated target value. So it's going to slowly do this over Time.deltaTime. So the camera is going to get this target position along with an offset.

[00:05:24] So the target position could be the player, it could be say where the collectible is, and then it needs to offset from that position by some amount, so it's not zoomed so close to it. And then we're going to move the camera in the x and z plane, kind of back and forth from its current position to that target over time.

[00:05:50] And if we wanted it to move faster or slower, we could multiply that by a speed value. When I was playing, I found that the default was actually pretty good. So we'll save that. We'll get some new serialized fields in this camera movement script. And for the target, let's try out the player and see what that looks like.

[00:06:18] And I'm gonna leave the board centre at its default value, and we'll see what that ends up looking like. So I've hit Space, Right now, I feel like that's pretty good. And it follows the player around so that you can see the action. If I change the x value of this offset, it actually won't do anything because that's calculated at the beginning.

[00:06:53] So let's maybe change that to, we'll say 0 and say -0.5. And that will kind of pull the camera back a little bit. So now we'll do that. And oops, actually, that moves the camera far forward. I meant to do the opposite and maybe a smaller value of 0.15, And that should pull it back towards us.

[00:07:22] Yeah, that's a little better. So I mean, you can play with this a number of ways. These are the types of things that you tend to tune when you're creating a game. I'm actually not sure if I liked that offset and that current framing, maybe that's something we'll change later.

[00:07:43] And I feel like maybe something else we could do is adjust the y. So like I said, lots of different ways we could play with that. Let's try changing the target from the player to the collectible. Just to see what that looks like.
>> Would it be hard to change the angles of the cameras instead?

[00:08:05] I mean, that sounds completely different thing, right?
>> Yeah, I mean that would get into
>> Yeah, so you see now, it is going to the collectible, but the movement is pretty jerky, and that's because it is looping at a constant speed. But if we were to learn say, using a different speed value might look a little bit better.

[00:08:30] So I'm gonna change that back to the player cuz I liked it the way it was.