直接在unity资源商店中搜索joystick,下载第一个
下载好之后导入,进入这个插件自带的场景,直接将canvas拖到下面,生成预制体
再进入你想加入摇杆的场景,直接拖到场景中,然后创建这个脚本
using UnityEngine;
namespace Cainos.PixelArtTopDown_Basic
{
public class JoystickPlayerExample : MonoBehaviour
{
public float speed = 5f;
public VariableJoystick joystick;
private Animator animator;
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
private void Update()
{
float horizontalInput = joystick.Horizontal;
float verticalInput = joystick.Vertical;
Vector2 direction = new Vector2(horizontalInput, verticalInput);
rb.velocity = direction * speed;
}
}
}
把脚本拖到人物上
然后摇杆就能控制人物移动了