Camera

9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€95.85
List Price:  €134.19
You save:  €38.34
£79.54
List Price:  £111.35
You save:  £31.81
CA$143.73
List Price:  CA$201.23
You save:  CA$57.50
A$159.93
List Price:  A$223.91
You save:  A$63.97
S$135.54
List Price:  S$189.77
You save:  S$54.22
HK$777.83
List Price:  HK$1,089
You save:  HK$311.16
CHF 89.34
List Price:  CHF 125.09
You save:  CHF 35.74
NOK kr1,131.88
List Price:  NOK kr1,584.68
You save:  NOK kr452.80
DKK kr715.08
List Price:  DKK kr1,001.15
You save:  DKK kr286.06
NZ$176.75
List Price:  NZ$247.46
You save:  NZ$70.70
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,945.33
List Price:  ৳16,723.94
You save:  ৳4,778.61
₹8,493.77
List Price:  ₹11,891.62
You save:  ₹3,397.85
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦154,938.50
List Price:  ₦216,920.10
You save:  ₦61,981.60
₨27,824.38
List Price:  ₨38,955.25
You save:  ₨11,130.86
฿3,419.79
List Price:  ฿4,787.84
You save:  ฿1,368.05
₺3,519.01
List Price:  ₺4,926.76
You save:  ₺1,407.74
B$608.53
List Price:  B$851.97
You save:  B$243.44
R1,830.80
List Price:  R2,563.19
You save:  R732.39
Лв187.32
List Price:  Лв262.25
You save:  Лв74.93
₩144,627.53
List Price:  ₩202,484.33
You save:  ₩57,856.80
₪365.39
List Price:  ₪511.57
You save:  ₪146.17
₱5,882.91
List Price:  ₱8,236.31
You save:  ₱2,353.40
¥15,643.93
List Price:  ¥21,902.13
You save:  ¥6,258.20
MX$2,007.58
List Price:  MX$2,810.70
You save:  MX$803.11
QR364.41
List Price:  QR510.19
You save:  QR145.78
P1,381.63
List Price:  P1,934.33
You save:  P552.70
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,087.81
List Price:  E£7,123.14
You save:  E£2,035.32
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$98,905.10
List Price:  CLP$138,471.10
You save:  CLP$39,566
CN¥729.56
List Price:  CN¥1,021.42
You save:  CN¥291.85
RD$6,087.13
List Price:  RD$8,522.23
You save:  RD$2,435.09
DA13,485.25
List Price:  DA18,879.89
You save:  DA5,394.64
FJ$231.62
List Price:  FJ$324.28
You save:  FJ$92.66
Q770.23
List Price:  Q1,078.35
You save:  Q308.12
GY$20,913.67
List Price:  GY$29,279.97
You save:  GY$8,366.30
ISK kr13,911.60
List Price:  ISK kr19,476.80
You save:  ISK kr5,565.20
DH1,006.07
List Price:  DH1,408.53
You save:  DH402.46
L1,836.70
List Price:  L2,571.46
You save:  L734.75
ден5,900.06
List Price:  ден8,260.33
You save:  ден2,360.26
MOP$800.26
List Price:  MOP$1,120.39
You save:  MOP$320.13
N$1,840.36
List Price:  N$2,576.58
You save:  N$736.22
C$3,678.42
List Price:  C$5,149.94
You save:  C$1,471.51
रु13,597.49
List Price:  रु19,037.04
You save:  रु5,439.54
S/372.23
List Price:  S/521.13
You save:  S/148.90
K405.36
List Price:  K567.52
You save:  K162.16
SAR375.62
List Price:  SAR525.88
You save:  SAR150.26
ZK2,766.46
List Price:  ZK3,873.15
You save:  ZK1,106.69
L477.10
List Price:  L667.96
You save:  L190.86
Kč2,408.98
List Price:  Kč3,372.68
You save:  Kč963.69
Ft39,689.03
List Price:  Ft55,566.23
You save:  Ft15,877.20
SEK kr1,103.13
List Price:  SEK kr1,544.43
You save:  SEK kr441.30
ARS$102,160.40
List Price:  ARS$143,028.65
You save:  ARS$40,868.24
Bs690.76
List Price:  Bs967.10
You save:  Bs276.33
COP$435,928.80
List Price:  COP$610,317.76
You save:  COP$174,388.96
₡50,434.81
List Price:  ₡70,610.75
You save:  ₡20,175.94
L2,537.51
List Price:  L3,552.62
You save:  L1,015.10
₲779,455.88
List Price:  ₲1,091,269.42
You save:  ₲311,813.53
$U4,474.38
List Price:  $U6,264.31
You save:  $U1,789.93
zł408.66
List Price:  zł572.14
You save:  zł163.48
Already have an account? Log In

Transcript

OK, so to get the camera movement happening, it's actually quite simple. So where we left off is when we play the game, our ball moves forward, we can move left and right with the controls the keyboard, and we can also do it on mobile. And but the camera doesn't actually follow the player. Now, this is obviously a problem. So there are a few ways we can handle this, we could actually just drag the play out. We could just start drag the camera and nested within the player, press play.

And it does something similar to that. And of course, we could combat that with code or just you know, make it you know, you could do some other stuff, but we're not going to do that. So we're just going to understand, we could go to Window, we've got a package manager, and we could actually click on all and we could download a package that there might even be included in your version of you leave here if you're watching this feature called cinema machine. So the machine used to be a paid actor. In the Asset Store, but now it is free for all unity developers because Unity has bought out the program because it was such a good plugin. It is the bee's knees if you want to make a camera for a 3d game, like God of War cinema machine if you want to make a game like Mario, and you need a camera for just a simple 2d game like that cinema machine, if you want a camera for a game like Gears of War, it shakes and or it looks really awesome.

It has some really cool cutscenes cinema sheen. It is extremely impressive and also extremely free, which is sweet. We could install that and if we wanted to, but for what we're doing, I don't actually think we need this much like grunt This is like putting a VA engine on a say tricycle. You don't really need it. Because all we're doing is moving the camera in one direction where it's not going to be doing anything else and it's actually really simple to do that. So we're going to do things the old fashioned way cuz I It'll also show some really cool some some really basic coding techniques that may help to understand things a little bit better.

So we're going to go over to our Scripts folder, we're going to right click, we're going to create a new C sharp script. And we're going to call this camera follow. They're gonna open up this. So there's a few things we need. The first thing we need is, we need to tell the script what we're trying to follow. So we're actually trying to follow the player.

And how do we know where the player is? Well, we know where the player is from the players transform position. So right now it's at 00, negative 41 45.1. Right? So we need to get that information. So we need a variable that can help us find information.

So we can just write public game object. So we're creating a game object variable. I'm just gonna call it player We don't need a stop method. So we can just get rid of this. don't even need this line of code here. It's just a comment, make our code extra lane.

And we're going to change update to late update, which I find works a little bit better for things like cameras, it's basically happens after update is called late update gets called. So we just go into another big one big long line of code. But hopefully this one isn't as confusing. It's very similar to the other line of code we wrote in the player script here, but it's just a little bit different, because we're not actually clamping anything. So all we're doing is we're saying, hey, this game object, sorry, this. Yeah, well, this camera just needs to move in a certain direction.

And so we want to smoothly move from its position and is now to the players position minus a certain amount of like, you know, distance between The camera in the player. So we could run game objects dot transform dot position. So this is talking about the main cameras transform position, which right now is this, this is the vector three. And we're saying gamma transform the position, remove that the common that was a mistake equals a vector, three dot loop. Okay, once again, looping is just smooth motion from one position to another. So we need a vector three a vector three B and how long it's going to get there.

So our first vector three position is just where we are now game objects dot transform dot position. And the next place we want to be is we could just write player position. But the problem with that is, well, I'll show you actually, let's run player dot transform dot position. Time Delta time, times 100, just to get the ball rolling. Cool. So if we press play, and we go to our main camera, and we drag our camera folder script over to the camera is going to actually ask us what the hell's the player.

I don't know what this is. So then we need to actually drag the player into that section. Camera for. So now, the script knows where the what the player game object is. And it's saying, hey, make my position. Lip it so smoothly move from my position to the players position and do it at time but over time, times 10 times 100.

Sorry. So let's see what happens. Press play. Oh, that's interesting. What's going on here? Okay, if we pause it and we zoom in, you'll see The map player is here for fun.

And our camera is actually right in the middle of the player, because we've just looked to the exact middle of the sphere. So things aren't working out exactly how we want it to them. So what we actually need to do is we need to create a bit of a buffer zone between the player and the camera. So how do we do that? Let's get back over to our script. And we're going to delete all this up to the vector three dot loop, open parentheses.

And we're going to say game object, dot transform position. This is what we want to look from. And we want to look to a new vector three. And the new vector three will equal zero. on the x axis, the wax will be damaged or transformed opposition dot y. So basically, don't worry about changing anything for our Hot for the camera hot.

Don't worry about changing the camera height, just keep it what it is. And the last one is player dot game object dot transform dot position dot z. And here's the here's the tricky bit minus 10. That's a pretty good distance run. Well, I think so. So basically what that means is, let's just end sorry, we'll just finish this on the code actually.

And then put a comma there and write time, dot delta time. times 100. just seemed like a good number before. Just so just bear just is there so it speeds it up. And with the end with parentheses, close the semi colon, one line of code. Let's see how it goes. So if we press play, Cool.

So our cameras, many don't even look. Yeah, there we go, you can see our camera is actually moving, then we can move left or right. And I can it's gonna fall off the edge. But if we press play again, you'll see that the big move left and right on the screen, and things seem to be working out pretty well. So, right now, if we get the player and we put the playback at zero, and we put the camera back at, well, it can stay at that height and we put that back at zero. They're both on top of each other, but it also drag this back to negative 10.

That would, that would mean that that's exactly if I press play, it should just start rolling on from exactly where we are because you know screen Script, that's actually what we've said, we've said, make a game on character make position, sorry, let from our position. And I'll just zoom out a little bit, make a position, look to a new vector three, the x position, don't worry about it, just leave that zero. The y position, don't worry about it, leave it at the default of whatever the cameras position is. And as the position leave it at the player's position, but minus 10. So if I made this 15, it obviously go back further, if I made it five, you'd go back further and do that at a rate of time. But though time times 100 awesome.

So now we have a working game in the fact that our working movement system in that in that our player can move left and right and continue on and he won't go off the screen. Obviously, if it's falling down now, and the camera will follow him or her soon the next video we'll do a little bit of a test.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.