Files
Echo/Assets/_Project/Scripts/Interaction/ItemPhysicsState.cs
T

50 lines
1.7 KiB
C#
Raw Normal View History

2026-07-08 19:53:15 +08:00
using UnityEngine;
namespace Project.Interaction
{
/// <summary>
/// 用于控制互动物品的初始物理状态。
/// 解决物品放置在只有一个碰撞体的架子上时被挤出的问题。
/// </summary>
public class ItemPhysicsState : MonoBehaviour
{
[Tooltip("如果开启,物体在场景初始化时会锁定物理(isKinematic=true),避免从架子上掉落。被玩家拾取后重新丢弃时,该状态会被自动解除。")]
public bool startKinematic = false;
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
if (rb == null)
{
Debug.LogWarning($"{nameof(ItemPhysicsState)} 需要 Rigidbody,但当前物体未找到(可能已在运行时被移除)。该组件将自动禁用。", this);
enabled = false;
return;
}
if (startKinematic)
{
rb.isKinematic = true;
}
}
/// <summary>
/// 供外部(如背包丢弃逻辑)调用,强制解除物理锁定,恢复受重力影响的正常物理状态
/// </summary>
public void ReleaseKinematic()
{
if (rb == null) rb = GetComponent<Rigidbody>();
if (rb == null)
{
Debug.LogWarning($"{nameof(ItemPhysicsState)}.ReleaseKinematic 调用失败:未找到 Rigidbody(可能已在运行时被移除)。", this);
return;
}
rb.isKinematic = false;
rb.useGravity = true;
rb.detectCollisions = true;
}
}
}