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

Unity玩家旋转解冻后无法再次冻结的问题求助

问题分析与解决方案

核心问题

  1. Rigidbody未初始化:你的GameManager脚本里没对rb变量赋值,导致设置freezeRotation时根本没作用到Player的刚体组件上。
  2. 方法逻辑与命名混淆:enableRotation的名字对应“允许旋转(解冻)”,但你代码里设的是freezeRotation = true(冻结旋转),逻辑错位会导致操作不符合预期。
  3. 组件调用方式不精准:用FindObjectOfType<GameManager>()在场景有多个同类脚本时容易出错,应该直接从触发的Player对象上获取组件。

修正后的代码

GameManager 脚本

using UnityEngine;

public class GameManager : MonoBehaviour
{
    private Rigidbody rb;

    void Start()
    {
        // 初始化获取自身的Rigidbody组件
        rb = GetComponent<Rigidbody>();
    }

    // 冻结玩家旋转
    public void FreezePlayerRotation()
    {
        if (rb != null && CompareTag("Player"))
        {
            rb.freezeRotation = true;
        }
    }

    // 解冻玩家旋转
    public void UnfreezePlayerRotation()
    {
        if (rb != null && CompareTag("Player"))
        {
            rb.freezeRotation = false;
        }
    }
}

flyer_Off 脚本

using UnityEngine;

public class flyer_Off : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        // 直接从进入触发器的Player对象上获取GameManager组件
        GameManager gameManager = other.GetComponent<GameManager>();
        if (gameManager != null)
        {
            // 根据你的需求,这里调用冻结或解冻方法,示例为冻结
            gameManager.FreezePlayerRotation();
        }
    }
}

额外注意事项

  • 确保Player对象上挂载了Rigidbody组件,且必要属性(比如Use Gravity)已正确设置。
  • 触发器立方体的碰撞体必须勾选Is Trigger,Player的碰撞体设为非触发器(特殊需求除外),同时两者的层级要设置为可相互触发。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 23:55:37