Help Needed with 2D Movement Script for Unity Game (Similar to Mini Militia)

I’m working on a 2D game in Unity and I’m trying to implement a movement script similar to the one in the game Mini Militia. I want the player to move smoothly using a joystick, with the ability to fly and walk. However, my current script isn’t working as expected. The player doesn’t turn quickly and the movement isn’t smooth.

<
using UnityEngine;

public class fly_test : MonoBehaviour
{
[SerializeField] Rigidbody2D rb;
public Joystick movementJoystick;
public float playerSpeed = 10f;
public float maxSpeed = 7f; // Maximum speed limit

public Vector2 movement;
public bool isJoystickReleased = false;
public bool wallking_mode = false;
bool is_on_the_ground = false;

private void Start()
{
}

private void Update()
{
    movement = new Vector2(movementJoystick.Horizontal, movementJoystick.Vertical);
    if (movement.magnitude == 0 && !isJoystickReleased)
    {
        isJoystickReleased = true;
    }
    else if (movement.magnitude > 0 && isJoystickReleased)
    {
        isJoystickReleased = false;
    }

    if (isJoystickReleased && is_on_the_ground)
    {
        wallking_mode = true;
    }

    float angle = Mathf.Atan2(movementJoystick.Vertical, movementJoystick.Horizontal) * Mathf.Rad2Deg;
    float degree = (angle + 360f) % 360f;
    if (degree > 30 && degree < 130)
    {
        wallking_mode = false;
        rb.freezeRotation = false;
        if (is_on_the_ground)
        {
            // rb.AddForce(Vector2.up * 100);
        }
    }
}

private void FixedUpdate()
{
    if (!wallking_mode)
    {
        rb.AddForce(movement * playerSpeed);
        // Limit the speed of the object
        rb.linearVelocity = Vector2.ClampMagnitude(rb.linearVelocity, maxSpeed);
        // Apply gravity when the joystick is released
        if (isJoystickReleased)
        {
            rb.gravityScale = 1f;
        }
        else
        {
            rb.gravityScale = 0f;
        }
    }
    else
    {
        if (!isJoystickReleased)
        {
            if (rb.gravityScale != 1.5)
            {
                rb.gravityScale = 1.5f;
            }
            Vector2 movement = new Vector2(movementJoystick.Horizontal * 3.5f, rb.linearVelocity.y);
            rb.linearVelocity = movement;
        }
    }
    backto_wallk_mode_if_it_collide_to_much_on_ground();
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("ground"))
    {
        is_on_the_ground = true;
    }
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.CompareTag("ground"))
    {
        // Debug.Log("Collision with player detected!");
    }
}

private void OnTriggerExit2D(Collider2D other)
{
    if (other.CompareTag("ground"))
    {
        wallking_mode = false;
    }
}

private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("ground"))
    {
        is_on_the_ground = false;
        force_the_ground = 0;
    }
}

float force_the_ground = 0;
void backto_wallk_mode_if_it_collide_to_much_on_ground()
{
    if (!wallking_mode && is_on_the_ground)
    {
        force_the_ground += Time.fixedDeltaTime;
        if (force_the_ground >= 0.3f)
        {
            wallking_mode = true;
            transform.rotation = Quaternion.identity;
            force_the_ground = 0;
        }
    }
}

}
/>

  1. How can I make the player turn more quickly and smoothly when changing direction?
  2. How can I improve the overall smoothness of the movement, especially when transitioning between flying and walking?

Instead of just using the raw velocity directly from what you compute out of the input system, you can smooth it first.

Smoothing the change between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub