Unity Mobile Joystick 2D :- This post will guide you through creating a virtual joystick for 2D games in Unity. You’ll learn how to design and implement a touch-friendly joystick, configure its movement and responsiveness, and integrate it seamlessly with your game’s controls. By the end, you’ll have a polished joystick system that offers precise and fluid player movement on mobile devices, improving the overall user experience.
Unity Joystick 2D Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JoystickController : MonoBehaviour
{
public Joystick movementJoystick;
public float playerSpeed;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (movementJoystick.Direction.y != 0)
{
rb.velocity = new Vector2(movementJoystick.Direction.x * playerSpeed, movementJoystick.Direction.y * playerSpeed);
}
else
{
rb.velocity = Vector2.zero;
}
}
}