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
€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 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.