Related
Are you new to Unity?
Looking to learn how to take inputs from players and move characters around the screen?
Let’s get started.
Unity makes this pretty easy as long as you know where to look.
With Unity open, click onEditat the top toolbar.
From the left-hand list, chooseInput Manager.
SelectAxes, populating the list of input values.
You’ll want to look atHorizontalandVerticalfor basic movement.
You’ll use these axes along withInput.GetAxisRaw();for basic movement in the next section.
In your Unity project’sHierarchyview, right-click and select3D Object > Capsuleto create what you’ll bestow movement upon.
ensure to use the same tactic to create a groundPlanefor your Capsule to stand on.
check that to reset theTransformvalueof both objects and move your Capsule so that it’s standing on the Plane.
Rename your Capsule to something likePlayerfor easy identification.
hit thePlayerobject and, in theInspectorview, scroll down toAdd Component.
Add aRigidbody, and then add another component as aCapsule Colliderthis time.
You’ll need these components to add physics, and therefore movement, to yourPlayer.
Then, right-click in yourScriptsfolder andCreatea newC# Script.
Name this script something along the lines ofPlayerMovement.
To achieve this, we’ll explore how to code movement in Unity using a simple player movement script.
By following these steps, you’ll learn how to make a character move in Unity effectively.
Utilizing Unity’s Rigidbody component will play a crucial role in achieving smooth and realistic Unity character movement.
Double-click your script to open it.
Thisspeedvariable is a multiplier that will control how quickly ourPlayermoves after some more programming.
For now, setspeedequal to something like10f.
You also need to let Unity that there’s aRigidbodyto manipulate in this script.
This is done with the keyword Rigidbody and a variable namewe’ll chooserb.
That’s all you should probably add in this section.
Now, move on tovoid Start().
This is the function you’ll use to constantly grabinputs from the players' keyboards, controllers, etc.
Remember when you checked theProject SettingsforInput Axes?
You’ll use them here.
Don’t worry if you’re feeling overwhelmed at the jump in code; we’ll explain it step-by-step.
Input.GetAxisRaw();is Unity’s way of recording Player inputs from theAxesyou found in theProject parameters.
you’re free to read more about it inUnity’s official documentation.
“Horizontal"comes from theHorizontal Axisname in Unity.
This axis controls left and right movement with the “a” and “d” keys.
Head back into Unity’sInspectorview for thePlayerobject.
Take a look at theRigidbodyunderInfo, you’ll see a valueVelocity.
This is the value you’re targeting withrb.velocity.
verify to drag thePlayerMovementscript onto thePlayerobject in Unity to enable smooth Unity player movement.
Unity Learn is chock-full of interesting topics to advance your game development skillset.
Stay curious and happy hacking!