← Back to list

Code Monkey Challenge Lab — 2D Movement

So, we have this simple 2D level and character.

Sean Duggan · 2026-05-19 22:09 · 0 claps · 2.8 min read
#unity #2d-game-development #code-monkey #challenge
Open on Medium ↗
Wiki topics: 🎮 · Gaming

Code Monkey Challenge Lab — 2D Movement

So, we have this simple 2D level and character.

The basic tasks are to provide 2D movement, and the ability to jump up or down through certain platforms. My first step was to give the character a 2D Rigidbody and 2D Box Collider.

You may notice that my box collider doesn’t reach all the way down. I want the character to go a bit into the ground to make it look a bit more natural. For input, I grab the existing Input Actions and I cue on Move being Performed or Cancelled to set a field for the current movement. I also store the associated Rigidbody.

void Awake()
{
    m_Actions = new InputSystem_Actions();
    m_Player = m_Actions.Player;

    m_Player.Move.performed += OnMove;
    m_Player.Move.canceled += OnMove;

    _rb = GetComponent<Rigidbody2D>();
}

For our first crude setup, I’ll move for whatever the current movement value is.

public void OnMove(InputAction.CallbackContext context)
{
    _movement = context.ReadValue<Vector2>();
}

private void Update()
{
    if (_movement.sqrMagnitude > 0.01f)
    {
        _rb.linearVelocity = _movement * _moveSpeed;
    }
}

Obviously, it lacks something. Kind of reminds me of playing E.T. on the Atari in some ways…

[embed]

First off, let’s only do the horizontal movement. Secondly, let’s use the current Animation Controller.

private void Update()
{
    if (_movement.sqrMagnitude > 0.01f)
    {
        Vector2 velocity = _movement * _moveSpeed;
        velocity.y = 0;
        _rb.linearVelocity = velocity;

        _animator.SetBool("IsRunning", true);
    } else
    {
        _animator.SetBool("IsRUnning", false);
    }
}

The speed isn’t right. Also, I forgot to fix the Rigidbody constraint so that it doesn’t rotate.

Hmm…. I need to flip the sprite based on direction. Also, my current method is zeroing out the vertical velocity so long as you’re moving. And lastly, we’re moonwalking… the last one is because I miscapitalized IsRunning. There’s also a fairly simple way to handle positioning by scaling the sprite based on the velocity sign.

private void Update()
{
    if (_movement.sqrMagnitude > 0.01f)
    {
        Vector2 velocity = _movement * _moveSpeed;
        _rb.linearVelocityX = velocity.x;

        _animator.SetBool("IsRunning", true);

        Vector3 scale = _sprite.transform.localScale;
        scale.x = velocity.x < 0 ? 1 : -1;
        _sprite.transform.localScale = scale;
    } else
    {
        _animator.SetBool("IsRunning", false);
    }
}

Things are a bit slidey because we only change velocity when we’re moving, and so we basically have only friction stopping us. We’ll just change the code to stop dead.

private void Update()
{
    if (_movement.sqrMagnitude > 0.01f)
    {
        Vector2 velocity = _movement * _moveSpeed;
        _rb.linearVelocityX = velocity.x;

        _animator.SetBool("IsRunning", true);

        Vector3 scale = _sprite.transform.localScale;
        scale.x = velocity.x < 0 ? 1 : -1;
        _sprite.transform.localScale = scale;
    } else
    {
        _rb.linearVelocityX = 0;
        _animator.SetBool("IsRunning", false);
    }
}

Next step will be jumping.


메타데이터
post_id
98dff263f5ee
slug
code-monkey-challenge-lab-2d-movement-98dff263f5ee
url
https://medium.com/@sean.duggan/code-monkey-challenge-lab-2d-movement-98dff263f5ee
canonical_url
https://medium.com/@sean.duggan/code-monkey-challenge-lab-2d-movement-98dff263f5ee
author_url
https://medium.com/@sean.duggan
status
ok
fetched_at
2026-06-09 15:37:30