You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity FPS游戏中如何用虚拟摇杆替代鼠标实现瞄准功能

Unity移动端:用虚拟摇杆替换鼠标实现瞄准功能

我是Unity新手,刚完成Udemy的课程,正在为游戏做移动端适配。已经成功用虚拟摇杆替换了PC端的移动输入:

//moveInput.x = Input.GetAxisRaw("Horizontal"); // PC
//moveInput.y = Input.GetAxisRaw("Vertical"); // PC

moveInput.x = movingJoystick.Horizontal; // Mobile
moveInput.y = movingJoystick.Vertical; // Mobile

现在需要把瞄准逻辑里的Vector3 mousePos = Input.mousePosition;换成虚拟摇杆输入(类似weapon.Joystick.Horizontal的形式),以下是处理角色和武器旋转的完整代码,求可行的实现方案:

if (canMove && !LevelManager.instance.isPaused)
{
    //moveInput.x = Input.GetAxisRaw("Horizontal"); // PC
    //moveInput.y = Input.GetAxisRaw("Vertical"); // PC

    moveInput.x = movingJoystick.Horizontal; // Mobile
    moveInput.y = movingJoystick.Vertical; // Mobile
    
    moveInput.Normalize();

    //transform.position += new Vector3(moveInput.x * Time.deltaTime * moveSpeed, moveInput.y * Time.deltaTime * moveSpeed, 0f);

    theRB.velocity = moveInput * activeMoveSpeed;

    Vector3 mousePos = Input.mousePosition; 

    Vector3 screenPoint = CameraController.instance.mainCamera.WorldToScreenPoint(transform.localPosition);

    if (mousePos.x < screenPoint.x)
    {
        transform.localScale = new Vector3(-1f, 1f, 1f);
        gunArm.localScale = new Vector3(-1f, -1f, 1f);
    }
    else
    {
        transform.localScale = Vector3.one;
        gunArm.localScale = Vector3.one;
    }

    //rotate gun arm
    Vector2 offset = new Vector2(mousePos.x - screenPoint.x, mousePos.y - screenPoint.y);
    float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
    gunArm.rotation = Quaternion.Euler(0, 0, angle);
}

实现方案

直接用瞄准摇杆的方向向量替代鼠标位置的计算逻辑,更适配移动端操作习惯,修改后的代码如下:

if (canMove && !LevelManager.instance.isPaused)
{
    // 移动逻辑保持不变
    moveInput.x = movingJoystick.Horizontal;
    moveInput.y = movingJoystick.Vertical;
    moveInput.Normalize();
    theRB.velocity = moveInput * activeMoveSpeed;

    // --- 替换鼠标输入为瞄准摇杆 ---
    // 假设你的瞄准摇杆引用为aimJoystick,根据实际变量名修改
    Vector2 aimInput = new Vector2(aimJoystick.Horizontal, aimJoystick.Vertical);
    
    // 处理摇杆无输入的情况,避免角度计算异常
    if (aimInput.magnitude < 0.1f)
    {
        // 无瞄准输入时,让武器朝向移动方向
        aimInput = moveInput;
    }
    aimInput.Normalize();

    // 角色翻转逻辑:根据瞄准方向的X轴正负判断
    if (aimInput.x < 0)
    {
        transform.localScale = new Vector3(-1f, 1f, 1f);
        gunArm.localScale = new Vector3(-1f, -1f, 1f);
    }
    else
    {
        transform.localScale = Vector3.one;
        gunArm.localScale = Vector3.one;
    }

    // 计算武器旋转角度
    float angle = Mathf.Atan2(aimInput.y, aimInput.x) * Mathf.Rad2Deg;
    gunArm.rotation = Quaternion.Euler(0, 0, angle);
}

关键说明:

  • 直接用摇杆方向计算角度:摇杆的Horizontal和Vertical本身就是方向向量,不需要再通过屏幕坐标转换,逻辑更简洁。
  • 无输入处理:增加了摇杆输入强度的判断,避免玩家没操作瞄准摇杆时,武器出现异常旋转,此时默认跟随移动方向。
  • 角色翻转逻辑优化:改为根据瞄准方向的X轴正负翻转角色,更符合移动端瞄准操作的直觉。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 12:27:24