"Distill your game down to that one thing—that singular piece of joy. Create that one joyful experience for your player, and nail it. That's your game."

Ryan Henson Creighton - President of Untold Entertainment

Making a Simple 2D Game



Character Movement Scripting

Next, we need to move on to the scripting side. Right-click in the asset window, and choose the create option, and then choose C#. If you integrated Visual studio during the Unity install, it should now open automatically with the scripting window. Now, name the script CharacterController, so that the following script will work (or choose a name of your own liking and change it accordingly). Create a script using the C# language.

We'll first go over how to make your character move. We’re going to post the full script here and go through it afterward.

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.


We'll now go over the script that we'll use for jumping. You don't need to make a completely separate script, just continue editing the previous one.

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.

If you encounter problems with game scripting in the future, Google is your best friend. Most likely someone has encountered a similar problem at some point, so 99% of the time the answer is just a Google search away.

Behold, Jump Guy is Here

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.