using UnityEngine;
public class CharacterController : MonoBehaviour
{
public float MovementSpeed = 1;
private void Start()
{
}
private void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
}
}
After doing this, you should be able to move your character from left to right with the arrow keys. Start by cleaning up the default script - Unity tends to automatically add lines that are not needed.
All the code is put inside the default Update() -function. It's automatically executed once per frame, so it's useful for checking the player's inputs and other checks that need to be made all the time.
Public float MovementSpeed = 1 adds a variable that you can change within Unity to set your movement speed. Var movement = Input.GetAxis("Horizontal") allows us to make a movement with the arrow keys possible. Transform.position += new Vector3(movement, 0, 0) creates a new Vector3 where the X is set to our input value. * Time.deltaTime * MovementSpeed multiplies the MovementSpeed -variable with the time elapsed since the last calculated frame. Now the player's speed is independent of the framerate of your device.
Now, take this script and add it to your character as a component. Test and see if it works - you should be able to move around. If you get compiling errors, go back to the script and make sure everything is done correctly.
using UnityEngine;
public class CharacterController : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
private Rigidbody2D _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
}
Private Rigidbody2D _rigidbody should be referenced here because we'll be using the rigidbody component multiple times during the script. As mentioned previously, rigidbody is very important when it comes to making the physics work with the player character.
We can use the default input system here for jumping, just as we did with movement, with the following command If (Input.GetButtonDown("Jump"). The second half, && Mathf.Abs (rigidbody.velocity.y) < 0.001f), limits the characters ability to jump, by checking the Y-axis. Now with rigidbody, we simply need to add a force to the Y-axis, and we'll need the JumpForce variable for this. It also serves as a float value, public float JumpForce = 1. The force we want to add needs to be an impulse.
You can now test out both movement and jumping in Unity. If you wish to change how high you can jump, you can do so from the component you've made. The same applies to movement speed. Just remember to close the "play mode" if it's still running. If you make changes while the mode is still active, the updates you make won't be saved outside of the play mode.
Jump Guy is finally worthy of their title. You can continue messing around with the movement values if you wish, but for the purpose of this course, this is enough. Below, you can see a video guide that recaps the entire process that we just went through.