Player Movement Control

11 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
€93.23
List Price:  €130.53
You save:  €37.29
£79.94
List Price:  £111.92
You save:  £31.98
CA$136.79
List Price:  CA$191.52
You save:  CA$54.72
A$153.20
List Price:  A$214.48
You save:  A$61.28
S$135.94
List Price:  S$190.32
You save:  S$54.38
HK$782.80
List Price:  HK$1,095.96
You save:  HK$313.15
CHF 91.30
List Price:  CHF 127.83
You save:  CHF 36.52
NOK kr1,094.90
List Price:  NOK kr1,532.91
You save:  NOK kr438
DKK kr695.39
List Price:  DKK kr973.58
You save:  DKK kr278.18
NZ$167.81
List Price:  NZ$234.95
You save:  NZ$67.13
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,958.03
List Price:  ৳15,341.69
You save:  ৳4,383.65
₹8,331.80
List Price:  ₹11,664.86
You save:  ₹3,333.05
RM477.70
List Price:  RM668.80
You save:  RM191.10
₦126,689.32
List Price:  ₦177,370.12
You save:  ₦50,680.80
₨27,798.13
List Price:  ₨38,918.49
You save:  ₨11,120.36
฿3,702.20
List Price:  ฿5,183.23
You save:  ฿1,481.03
₺3,249.97
List Price:  ₺4,550.10
You save:  ₺1,300.12
B$514.66
List Price:  B$720.55
You save:  B$205.88
R1,901.16
List Price:  R2,661.70
You save:  R760.54
Лв182.41
List Price:  Лв255.39
You save:  Лв72.97
₩137,421.64
List Price:  ₩192,395.79
You save:  ₩54,974.15
₪379.55
List Price:  ₪531.39
You save:  ₪151.83
₱5,778.57
List Price:  ₱8,090.23
You save:  ₱2,311.66
¥15,552.99
List Price:  ¥21,774.81
You save:  ¥6,221.82
MX$1,705.36
List Price:  MX$2,387.58
You save:  MX$682.21
QR364.16
List Price:  QR509.84
You save:  QR145.68
P1,384.82
List Price:  P1,938.81
You save:  P553.98
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,789.16
List Price:  E£6,705.01
You save:  E£1,915.85
ብር5,677.33
List Price:  ብር7,948.49
You save:  ብር2,271.15
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$95,258.47
List Price:  CLP$133,365.67
You save:  CLP$38,107.20
CN¥724.52
List Price:  CN¥1,014.36
You save:  CN¥289.84
RD$5,872.31
List Price:  RD$8,221.47
You save:  RD$2,349.16
DA13,431.87
List Price:  DA18,805.15
You save:  DA5,373.28
FJ$229.18
List Price:  FJ$320.86
You save:  FJ$91.68
Q776.12
List Price:  Q1,086.60
You save:  Q310.48
GY$20,901.55
List Price:  GY$29,263
You save:  GY$8,361.45
ISK kr13,994.60
List Price:  ISK kr19,593
You save:  ISK kr5,598.40
DH1,013.23
List Price:  DH1,418.56
You save:  DH405.33
L1,780.93
List Price:  L2,493.37
You save:  L712.44
ден5,743.61
List Price:  ден8,041.28
You save:  ден2,297.67
MOP$805.67
List Price:  MOP$1,127.97
You save:  MOP$322.30
N$1,914.68
List Price:  N$2,680.63
You save:  N$765.95
C$3,675.06
List Price:  C$5,145.23
You save:  C$1,470.17
रु13,310.19
List Price:  रु18,634.81
You save:  रु5,324.61
S/370.43
List Price:  S/518.62
You save:  S/148.18
K379.55
List Price:  K531.39
You save:  K151.83
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,608.51
List Price:  ZK3,652.02
You save:  ZK1,043.50
L463.96
List Price:  L649.56
You save:  L185.60
Kč2,349.11
List Price:  Kč3,288.85
You save:  Kč939.73
Ft36,594.60
List Price:  Ft51,233.91
You save:  Ft14,639.30
SEK kr1,086.27
List Price:  SEK kr1,520.82
You save:  SEK kr434.55
ARS$87,315.45
List Price:  ARS$122,245.13
You save:  ARS$34,929.67
Bs689.99
List Price:  Bs966.01
You save:  Bs276.02
COP$389,137.18
List Price:  COP$544,807.62
You save:  COP$155,670.43
₡50,081.85
List Price:  ₡70,116.60
You save:  ₡20,034.74
L2,464.83
List Price:  L3,450.86
You save:  L986.03
₲741,742.01
List Price:  ₲1,038,468.49
You save:  ₲296,726.47
$U3,834.62
List Price:  $U5,368.62
You save:  $U1,534
zł402.27
List Price:  zł563.20
You save:  zł160.92
Already have an account? Log In

Transcript

OK, so to get some movement happening, let's double click on our player script. And we're going to create a couple of variables up at the top here, we're going to create a public float. And we're going to call this player speed. If you start with a small p or small letter, and then you, whenever you write a, a uppercase letter, it will actually split that. And the reason we say public is so that we can access that in the inspector. So if we go over to the inspector and go over to the player, you'll see that we have now a player speed variable here, and you'll see that it's good at player speed and it looks nicely formatted.

So these are just coding best practices. And we want to create a we don't need to access this in. Oh, yeah, actually, we'll create a public access. We'll make it with public, public float, directional speed. Okay, so now we have two variables player speed, and direction speed. So player speed we'll deal with like how fast the actual ball is, the player is moving forward.

And the directional speed will be how fast it moves left and right. So we actually already using that we're already using this number somewhere run. So we're actually using it down here. We're timesing vector three dot followed by 1000. We can change this thousand to play a speed. And if we press Save, and we go back over to our game, you'll see that will have two variables here pop up.

And if we press play, you will notice something very interesting. First of all, nothing happens. Why is that? Nothing's happening is because the public float player speed we are now timesing, vector three to four by player speed, and play speed currently equals zero. So what happens in my When we taught something buzzer, it equals zero. So if we actually, we can actually change this in here.

So we can actually drag this forward and forward 444. And you'll notice that at 184, that's how fast our player is moving, we can make this 1000. And that's how fast it was moving before. Oh, we could actually make it negative 500. And, yeah, they're all numbers, they all do stuff. And negative 500 means it's actually moving backwards.

But you can see now it's under the floor, you get some wacky results, we can actually create the default in here. So let's say because we're using 1000, before, that was a little bit faster, let's say 500. So we can say equals 500. So now it should default is 500. If we want to go back over to the player script, we can click Reset, and it will cost us 500. Now whatever adjustments we make here will then override whatever number is in here.

So just make sure sometimes it's a bit of a gotcha. And you'll be wondering, why isn't my script working, just make sure that this number here, or this number here more is the number that you actually want it to be. Cool. So the next thing we want to do is we want to actually create a float. Now this float will only be used inside of updates. So we don't need to actually declare it up here because it's not a class variable.

It's not a variable that's gonna be used for the whole class. We're just going to be using it once and then basically throwing it out. So we're going to create float, and we're going to call it move or is zone tool. I was told that run and we're going to equal that to input dot get access, open parentheses, and then we're going to open speech quotes. And we're going to talk with a capital horizontal and Once again, close every line with the code with some a column. And we're just going to debug that out debug dot log, move horizontal, so we can see what's going on.

So if we press play, ignore the, the game of brick for a moment, and we're going to go over to our console, we'll see that we have zero. Now if you press right on your, on your keyboard, so click on the play game, it was brought on the keyboard, you'll see that it goes up to one and negative one. And you know, it changes when you press left and right on the keyboard. This is because we're accessing the Unity inbuilt function called input. And we're getting information from the keyboard and we're assigning it to a variable called float float variable called moving horizontal. So we can get rid of this debug dot log.

And then the next part might be a little bit confusing. We're going to run transform dot position. So what is the transform dot position. So if we go back over to our player, we'll get a bunch of errors pop up, if you've just done that don't need to follow along, but just watch. If we go over to our player, we'll see we've got our transform up to here. So once again, we're accessing something on the game object, we're accessing the Transform, and we're accessing the position.

So we've got an x value. We've got a y value, which is up and down, and we've got a Z value, which is zero goes out. And we go back over the script, and we're saying transform dot position. So we're, we're saying what we want to access this thing. And we're going to equal that to something. And what are we going to equal it to?

Well, we're going to equal to, we could equal it to back to three or four or something, we could equal it to a bunch of different things. But what we want to do is we want equal to a new position. So what we're going to say is vector three, don't look So lookup is linear interpolation. So basically is a smooth movement between two different spots. And right now you can see it's asking for a vector three a position, a vector three B position and a time it's going to take to get there. So this is confusing you, I don't blame you.

This is the kind of stuff that you learn while you, you learn more and more as you get better programming. This is just the syntax that unity uses to control certain things. And if you're brand new to programming, all of this will go right over your head. But just follow along, do exactly what I do. And we'll get there in the end. So the first position we want to learn from well is just the position that we're at.

So we can say game object, dot transform, dot position. And then where do we want to go? Well, we want to go to a new vector three. So this is where it gets even extra tricky. So for example, we want our player to be able to if you watch this, this game view here, we want the player to be able to move forward So what we can actually play because we're getting some errors, we want to set the player to move in this direction here, right? Well, this is actually backwards.

So we want it to move forward. And we want it to be able to move left, left and right with the controls. But we don't want to be able to move all the way off the screen like we're way off here, because that would not be fun. So we want to actually clamp the position of where can actually move to, like, say, maybe here, and maybe here, so it can only move to the different sides of the screen. But we want to be able to move as far as it wants to in this direction. So there's one line of code will do all that for us.

So it's a pretty sweet line of code. So we want to access transform position equals vector three dot lookup game object dot transform dot position, which is a position now we want to change that we want to move from our current position from a game or transform to a position to a new vector three. We want to clamp it into victory with math, F dot clamp and it's asking will What do we want to clamp, we want to clamp the game object dot transform dot position dot x. So we're accessing the x variable variable plus move horizontal. So we're now accessing the variable that we declared up here, which is our horizontal axis. put a comma in, and I'm going to do minus 2.5 F. So that's minus 2.5.

And because we're using a float, you put the F after one, comma 2.5. Awesome. So that then clamps position and then we want to go past the parentheses and put a comma in. And we want to run game objects dot transform dot position, dot y, remember where it's asking us for a new vector three. Oh, and we forgot the F here. And then we want to say demo tricks dot transform.

Dot position dot z, close parentheses, comma, directional speed, which is the variable we declared at the top times time, dot delta time. And we want to close those parentheses. So I'm going to zoom out a little bit just so we can see the whole line of code. And it's quite a long line of code. So we're accessing the transform position we're creating. And we're saying hey, that the transform position of our of our play a game object equals a new war evils vector three dot loop, we're looping which is moving smoothly from one position to another.

The first position we're moving from is the current position of our play a game object, we're moving to a new position, which is a new vector three. That was that the x value on that vector three is clamped between negative 2.5 and 2.5. Now y position is just whatever the y position is. The z position is whatever the Z position is. And we're timesing that by directional speed times time dot delta time. So the only other thing we need to do is a sign our directional speed.

So we can go up here and write equals, say 20. And get back over to unity. Make sure it updates, reset. And if we press play, we should be able to move left and right. And I'm just going to right click on the same view and whip around just so I can see in the direction, see the ball in the direction that we're actually moving. And let's drag and Rod back and let's just get the play the camera and Drag it right back, we can actually bring it up a little bit.

And we can actually point it down a little bit. Cool. And if we press play, move left and rotten, right and it stops at a certain position. So if this episode went away of your head, I don't blame you. This is one long line of code, but it's just a really quick and neat line of code. If your project isn't working properly, make sure you copy everything exactly the same, the capitals may be the same, the points will be the same.

You need to use commas at the right positions. And you need to be using everything here type exactly the same and it definitely will work if you've got everything typed exactly how it is here in this script. So that's it for getting the plate player to move left and run forward. In the next episode, we'll handle mobile controls.

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.