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

Unity构建中部分对象Update()仅在推力输入时触发旋转问题求助

Unity构建后部分对象Update旋转仅在玩家推力输入时生效的排查方案

问题场景

编辑器中运行正常,但最新构建版本出现以下异常:

  • 部分挂载了Update旋转逻辑的对象(如可收集燃料、大型障碍物),仅在触发玩家推力输入时才执行旋转
  • 同一场景内仅部分对象出现该问题,其余正常;跨场景的异常对象类型也不一致
  • 数月未修改旋转脚本与推力函数,数周前的构建版本无此问题

异常旋转代码

// Update is called once per frame
void Update () 
{
    //Set constant rotation
    gameObject.transform.Rotate(Vector3.up * rotationSpeed);
}

推力函数代码(供参考)

void Thrust()
{
    if(state == State.Alive)
    {   
        if(Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Mouse0) || InputManager.AButton())
        {
            childLight.enabled = true;
            fuelTotal--; 

            rigidbody.AddRelativeForce(Vector3.up * primaryThrust * Time.deltaTime);
            if(!audioSource.isPlaying)
            {   
                if(submerged == false)
                {
                    audioSource.clip = thrustSound;
                    audioSource.Play();
                    thrustParti.Play();
                    thrusting = true;
                }
                else
                {
                    audioSource.clip = bubbles;
                    audioSource.Play();
                    bubbleParti.Play();
                    thrusting = true;
                }
            }
        }
        else
        {
            audioSource.Stop();
            thrustParti.Stop();
            bubbleParti.Stop();
            childLight.enabled = false;
            thrusting = false;

            //ScaleMaskDown();
        }
    }
    else
    {
        childLight.enabled = false;
    }
}

排查方向与解决方法

  1. 脚本/对象激活状态异常

    • 给异常对象的Update函数添加日志,确认非推力输入时Update是否被调用:
      Debug.Log($"[{gameObject.name}] Update called, script enabled: {enabled}, object active: {gameObject.activeSelf}", this);
      
    • 若日志未输出,排查是否有其他脚本在非推力时禁用了对象或脚本,检查场景加载逻辑是否存在激活状态的异常修改。
  2. 构建优化设置导致的代码异常

    • 打开PlayerSettings > Other Settings:
      • 将Script Call Optimization改为Slow and Safe(Fast模式下Unity可能对无外部引用的代码做激进优化)
      • 将Managed Stripping Level改为Low,避免不必要的代码被剥离
    • 重新构建测试,若问题消失,说明是优化设置导致的异常。
  3. Time.timeScale被意外修改

    • 排查项目中所有修改Time.timeScale的代码(如暂停逻辑、UI脚本),确保非推力输入时Time.timeScale保持为1。虽然Update不受timeScale直接影响,但部分依赖时间的间接逻辑可能受此牵连。
  4. 变量序列化丢失

    • 在异常对象的Awake/Start函数中打印rotationSpeed的值,确认构建后该变量是否被重置为0:
      void Start()
      {
          Debug.Log($"[{gameObject.name}] rotationSpeed: {rotationSpeed}", this);
      }
      
    • 若值异常,检查变量是否添加[SerializeField]或设为public,确保Inspector中的赋值能正确序列化到构建版本中。
  5. 父对象旋转逻辑覆盖

    • 检查异常对象的父节点是否存在强制设置旋转的逻辑(如transform.rotation = Quaternion.identity),若父节点在非推力时锁定旋转,会导致子对象的旋转被覆盖,仅当父节点逻辑暂停时(如推力输入触发)子对象旋转才生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 09:25:57