Basic Player Movement
We’re finally ready to start writing some code! Create a new Script PlayerMovement.cs in your scripts folder. Add this to your Player prefab (or to the one in your scene, but make sure to apply your changes at the end!)
First add a new variable to store a reference to the RigidBody. In Awake() set this using GetComponent().
Horizontal Movement
We are also going to need a public float to control the player’s move speed. Set its value to 5 for now.
Now that we have access to the RigidBody, we can update the rigid.velocity. Set a new Vector3 equal to the current velocity. Update the x component of this to be equal to the Input on the Horizontal axis multiplied by the move speed.
Vertical Movement
The first thing you may think to do is add gravity. However, Rigidbodies default to having gravity by default.
But we still need to add jumping. Just as you did with moveSpeed, make a public jumpPower variable and set it to 15.
Next check for the Z key being pressed and set the velocity to the jumpPower when this happens.
Conclusion
And that’s it for movement, run your game and you’ll see that… there are a lot of problems…
But that’s OK because in the next tutorial we’ll be going over how to fix these problems. When you’re ready commit your changes and go to 07-Player Movement Fixes.