Essentially, line 12:moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); is getting the horizontal and vertical “axis” values from the Input class. This will generally be -1 to 1 for keyboards and joysticks:
- Vertical - Up = 1
- Vertical - Down = -1
- Horizontal - Right = 1
- Horizontal - Left = -1
This is being used to correspond to Vector3 directions. Vector3s represent either a position or a direction in 3D space. It’s saying “whatever our horizontal input is, from -1 to 1, that’s going to be our X direction (left or right), and whatever our vertical input is, from -1 to 1, that’s going to be our Z direction (back or forward)”.
It’s worth noting here that the coordinate system in Unity is set up such that:
- Forward = +Z
- Back = -Z
- Right = +X
- Left = -X
- Up = +Y
- Down = -Y
And you can access these directions very quickly with the Vector3 shorthand members of “Vector3.forward”, “Vector3.right”, “Vector3.up”, etc… (Vector3.forward, for instance has x / y / z values which are “0 / 0 / 1” which is +Z).
And it’ll make a lot of sense the more you think about it. The next thing you have to think about is the difference between a local direction and a world direction. Say you have a character who’s standing right on the origin point (0,0,0) in world space and facing north (forward, or +Z). To that character, local space and world space mean the same thing- “to his right” means +X, and “behind him” means -Z.
Now turn the character so that he’s facing left. Suddenly his “to his right”, his personal +X, is actually +Z in world space.
Line 13:moveDirection = transform.TransformDirection(moveDirection); transform.TransformDirection(), turns the “transform”'s personal directions into world directions, so by saying “transform.TransformDirection(Vector3.forward)” which is a shorthand way of writing “+Z” as a direction (personal “forward”), you’re translating what “forward” means to that transform into what that direction is in world space, which can be anything depending on how the object is rotated. The result is always going to be a unit value though (like +1Z or -1Z), so in order to vary how far you’re going to move in that direction, you have to multiply that by speed, which is what they do next on line 14:moveDirection *= speed;
Line 19:moveDirection.y -= gravity * Time.deltaTime; pulls the object down in the global -y direction (“down” in the global sense, not the object’s personal sense) in order to simulate the effect of gravity. This is useful to keep your object firmly planted on terra firma- or “drop” if you step off a ledge or jump. The value set for gravity there is 20f, so essentially “20 units per second”, which is sort of arbitrary but works well as long as you have an object that’s roughly 1-2 units tall, and not 50. Time.deltaTime is actually “the amount of time the last frame took to process”, so it’s a great way to smooth things out and make them framerate-independent, meaning that your game won’t actually move faster with a higher framerate or slower with a lower framerate.
Hope that helps!