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

武器动画方向与firePoint鼠标定位不一致问题技术求助

问题描述

我的武器有对应鼠标方向(左、右、右上、下等)的动画,但存在一个问题:移动鼠标时,firePoint GameObject会跟随鼠标位置移动,导致枪械射击方向与动画方向不匹配。

当前使用基于动画混合树(Blend Tree)的代码控制动画:

void UpdateAnimation()
{
    Vector3 mouseLocationWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    mouseLocationWorld.Normalize();
    if (mouseLocationWorld.x == 0f && mouseLocationWorld.y == 0f)
    {
        _animator.SetFloat("LastHorizontal", mouseLocationWorld.x);
        _animator.SetFloat("LastVertical", mouseLocationWorld.y);
    }
    else
    {
        lastX = mouseLocationWorld.x;
        lastY = mouseLocationWorld.y;
    }
    _animator.SetFloat("Horizontal", mouseLocationWorld.x);
    _animator.SetFloat("Vertical", mouseLocationWorld.y);
}
解决方案

问题核心是firePoint的驱动逻辑和动画方向逻辑不一致,导致两者错位。以下是两种可行的解决方式:

方式一:复用动画参数同步firePoint方向

既然已经通过Horizontal和Vertical参数控制动画方向,直接复用这些参数来计算firePoint的朝向,确保两者完全对齐:

void UpdateFirePointDirection()
{
    Vector3 currentDir = new Vector3(_animator.GetFloat("Horizontal"), _animator.GetFloat("Vertical"), 0);
    
    // 当当前方向无效时,使用上一次保留的方向
    if (currentDir.magnitude < 0.1f)
    {
        currentDir = new Vector3(_animator.GetFloat("LastHorizontal"), _animator.GetFloat("LastVertical"), 0);
    }

    // 设置firePoint的朝向(2D场景用right,3D场景根据需求调整为forward等)
    firePoint.transform.right = currentDir.normalized;
}

在Update方法中同时调用UpdateAnimation()和UpdateFirePointDirection()即可。

方式二:将firePoint设为动画模型的子物体

如果希望firePoint的位置和方向完全贴合动画中的枪械姿态,最稳妥的做法是把firePoint直接挂载到武器动画模型的枪口骨骼/Transform节点下。这样动画播放时,firePoint会自动跟随动画的位移和旋转,无需额外代码控制位置,只需确保初始位置在枪口即可。

关键注意点

  • 禁止直接用鼠标世界位置驱动firePoint的位置,这会让它脱离动画的约束框架。
  • 确保LastHorizontal和LastVertical的逻辑正常,避免鼠标方向归零后firePoint出现异常回正。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 18:45:49