You need to enable JavaScript to run this app. Unity使用EasyJoystick与Mirror实现触摸射击时如何高效排除摇杆区域触摸输入
优惠活动
大模型
产品
解决方案
定价
更多

Unity使用EasyJoystick与Mirror实现触摸射击时如何高效排除摇杆区域触摸输入

排除摇杆区域触摸的高效实现方案

核心思路是通过预计算摇杆的屏幕触摸区域,在判断射击触发条件时直接过滤掉落在该区域内的触摸,性能开销极低,不需要做射线检测等耗时操作。同时还能修复现有代码中仅识别第一个触摸、多手指操作失效的问题。


实现步骤

  1. 引用摇杆触摸区域
    首先找到项目中EasyJoystick用于接收触摸的RectTransform节点(通常是Canvas下命名为JoystickArea或类似的根节点),把它拖到ShootBullet脚本的对应序列化字段中。
  2. 预计算摇杆的屏幕空间矩形
    摇杆区域的位置大小一般不会动态变化,所以只需要在脚本初始化时计算一次它的屏幕坐标范围,避免每次Update重复计算浪费性能。
  3. 过滤无效触摸
    遍历所有触摸点,跳过所有落在摇杆区域内的触摸,剩下的非摇杆触摸才用于触发射击。

改造后的完整脚本

public class ShootBullet : NetworkBehaviour
{
    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private RectTransform joystickArea; // 拖入EasyJoystick的触摸区域
    private GameObject bullet;

    // 提前计算的摇杆屏幕区域
    private Rect joystickScreenRect;
    // Set via the Inspector in Units/second
    [SerializeField] private float _moveSpeed = 2f;
    [SerializeField] private Camera _camera;
    Vector3 touch_Pos = new Vector3(0, 0, 0);

    private void Awake()
    {
        if (!_camera) _camera = Camera.main;
        
        // 仅初始化时计算一次摇杆屏幕区域
        if (joystickArea != null)
        {
            Vector3[] joystickCorners = new Vector3[4];
            joystickArea.GetWorldCorners(joystickCorners);
            // 转换为屏幕坐标下的矩形(触摸点的坐标和该Rect坐标系一致)
            joystickScreenRect = Rect.MinMaxRect(
                joystickCorners[0].x,
                joystickCorners[0].y,
                joystickCorners[2].x,
                joystickCorners[2].y
            );
        }
    }

    void Update()
    {
        if (!this.isLocalPlayer) return;

        // 遍历所有触摸,逐个过滤摇杆触摸
        for (int i = 0; i < Input.touchCount; i++)
        {
            Touch currentTouch = Input.GetTouch(i);
            
            // 跳过落在摇杆区域内的触摸
            if (joystickScreenRect.Contains(currentTouch.position))
            {
                continue;
            }

            // 非摇杆区域的开始触摸,才触发射击
            if (currentTouch.phase == TouchPhase.Began)
            {
                touch_Pos = _camera.ScreenToWorldPoint(currentTouch.position);
                touch_Pos.z = 0; // 2D项目需补设Z轴为0,避免子弹位置偏移
                this.CmdShoot();
                break; // 一次触发只发射一发,避免重复调用
            }
        }

        // 原有子弹移动逻辑
        if (bullet != null)
        {
            bullet.transform.position = Vector3.MoveTowards(bullet.transform.position, touch_Pos, _moveSpeed * Time.deltaTime);
        }
    }

    [Command]
    void CmdShoot()
    {
        bullet = Instantiate(bulletPrefab, this.transform.position, Quaternion.identity);
        NetworkServer.Spawn(bullet);
    }
}

额外优化提示

  • 原有脚本仅用单个bullet变量跟踪子弹,多次射击时旧子弹的移动逻辑会被覆盖,仅最新生成的子弹会移动。如果需要同时存在多颗子弹,可以改用List<GameObject>存储所有生成的子弹,或把子弹移动逻辑写到子弹自身的脚本中。
  • 如果后续需要排除其他UI按钮的触摸,只需在判断时额外添加对应UI的Rect检测即可,扩展性强。

注意:如果你的EasyJoystick设置为「动态摇杆」(触摸任意区域才生成摇杆),需要把计算joystickScreenRect的逻辑从Awake移到每次触摸触发时,适配动态范围的变化。


内容的提问来源于stack exchange,提问作者Newboy11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 00:45:04