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
€94.79
List Price:  €132.71
You save:  €37.92
£78.97
List Price:  £110.56
You save:  £31.59
CA$139.89
List Price:  CA$195.85
You save:  CA$55.96
A$153.73
List Price:  A$215.23
You save:  A$61.50
S$134.32
List Price:  S$188.05
You save:  S$53.73
HK$778.26
List Price:  HK$1,089.59
You save:  HK$311.33
CHF 88.57
List Price:  CHF 124
You save:  CHF 35.43
NOK kr1,105.08
List Price:  NOK kr1,547.16
You save:  NOK kr442.07
DKK kr706.99
List Price:  DKK kr989.82
You save:  DKK kr282.82
NZ$170.06
List Price:  NZ$238.10
You save:  NZ$68.03
د.إ367.26
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,906.24
List Price:  ৳16,669.22
You save:  ৳4,762.97
₹8,436.24
List Price:  ₹11,811.08
You save:  ₹3,374.83
RM447.10
List Price:  RM625.96
You save:  RM178.86
₦167,464.25
List Price:  ₦234,456.65
You save:  ₦66,992.40
₨27,687.73
List Price:  ₨38,763.93
You save:  ₨11,076.20
฿3,470.35
List Price:  ฿4,858.63
You save:  ฿1,388.28
₺3,445.98
List Price:  ₺4,824.51
You save:  ₺1,378.53
B$578.68
List Price:  B$810.17
You save:  B$231.49
R1,810.53
List Price:  R2,534.81
You save:  R724.28
Лв185.34
List Price:  Лв259.49
You save:  Лв74.14
₩139,691.42
List Price:  ₩195,573.58
You save:  ₩55,882.15
₪373.67
List Price:  ₪523.15
You save:  ₪149.48
₱5,901.36
List Price:  ₱8,262.14
You save:  ₱2,360.78
¥15,574.86
List Price:  ¥21,805.43
You save:  ¥6,230.57
MX$2,020.46
List Price:  MX$2,828.72
You save:  MX$808.26
QR363.35
List Price:  QR508.71
You save:  QR145.35
P1,355.53
List Price:  P1,897.79
You save:  P542.26
KSh12,948.70
List Price:  KSh18,128.70
You save:  KSh5,180
E£4,969.50
List Price:  E£6,957.50
You save:  E£1,988
ብር12,262.61
List Price:  ብር17,168.15
You save:  ብር4,905.53
Kz91,256.94
List Price:  Kz127,763.36
You save:  Kz36,506.42
CLP$97,119.28
List Price:  CLP$135,970.88
You save:  CLP$38,851.60
CN¥724.58
List Price:  CN¥1,014.45
You save:  CN¥289.86
RD$6,000.34
List Price:  RD$8,400.72
You save:  RD$2,400.37
DA13,341.73
List Price:  DA18,678.95
You save:  DA5,337.22
FJ$227.04
List Price:  FJ$317.87
You save:  FJ$90.82
Q769.16
List Price:  Q1,076.85
You save:  Q307.69
GY$20,834.51
List Price:  GY$29,169.15
You save:  GY$8,334.63
ISK kr13,772.62
List Price:  ISK kr19,282.22
You save:  ISK kr5,509.60
DH995.44
List Price:  DH1,393.66
You save:  DH398.21
L1,819.82
List Price:  L2,547.83
You save:  L728
ден5,825.40
List Price:  ден8,155.79
You save:  ден2,330.39
MOP$798.74
List Price:  MOP$1,118.27
You save:  MOP$319.52
N$1,801.35
List Price:  N$2,521.97
You save:  N$720.61
C$3,666.61
List Price:  C$5,133.40
You save:  C$1,466.79
रु13,462.23
List Price:  रु18,847.67
You save:  रु5,385.43
S/378.10
List Price:  S/529.35
You save:  S/151.25
K400.90
List Price:  K561.28
You save:  K160.37
SAR375.38
List Price:  SAR525.55
You save:  SAR150.16
ZK2,752.36
List Price:  ZK3,853.42
You save:  ZK1,101.05
L471.69
List Price:  L660.38
You save:  L188.69
Kč2,395.79
List Price:  Kč3,354.20
You save:  Kč958.41
Ft38,840.72
List Price:  Ft54,378.57
You save:  Ft15,537.84
SEK kr1,099.74
List Price:  SEK kr1,539.68
You save:  SEK kr439.94
ARS$100,165.56
List Price:  ARS$140,235.79
You save:  ARS$40,070.23
Bs690.96
List Price:  Bs967.37
You save:  Bs276.41
COP$440,690.82
List Price:  COP$616,984.78
You save:  COP$176,293.95
₡50,645.88
List Price:  ₡70,906.26
You save:  ₡20,260.37
L2,517.41
List Price:  L3,524.47
You save:  L1,007.06
₲775,966.15
List Price:  ₲1,086,383.66
You save:  ₲310,417.50
$U4,274.58
List Price:  $U5,984.58
You save:  $U1,710
zł411.21
List Price:  zł575.71
You save:  zł164.50
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.