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

如何基于手柄右摇杆位置实现GameObject绕另一物体旋转?

问题原因与修复方案

你的手柄逻辑核心错误在于:摇杆输出的是**-1到1的方向向量**,不是鼠标那样的屏幕像素坐标,直接用ScreenToWorldPoint转换完全不匹配,这就是手柄端失效的根本原因。

修正后的代码

if (gameObject.name == "Player")
{
    mousePos = Mouse.current.position.ReadValue();
    mousePos.z = cam.nearClipPlane;
    Vector3 worldpos = cam.ScreenToWorldPoint(mousePos);
    Vector3 aimDirection = (worldpos - transform.position).normalized;
    float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;
    aim.eulerAngles = new Vector3(0, 0, angle);
}
else
{
    // 直接使用摇杆输出的方向向量,无需转屏幕坐标
    Vector2 stickDirection = controllerRightStick.ReadValue<Vector2>();
    // 可选:摇杆死区处理,避免轻微晃动导致角度异常
    if (stickDirection.magnitude < 0.1f) return;
    
    // 用摇杆的x/y直接计算角度,和鼠标转向逻辑对齐
    float angle = Mathf.Atan2(stickDirection.y, stickDirection.x) * Mathf.Rad2Deg;
    aim.eulerAngles = new Vector3(0, 0, angle);
}

关键改动说明

  • 移除手柄部分的ScreenToWorldPoint转换,直接复用摇杆本身的方向信息
  • 增加可选死区判断,过滤摇杆归位时的微小输入,避免角度跳变
  • 摇杆角度计算逻辑和鼠标端完全对齐,保证两名玩家的操控体验一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 08:05:21