Fix chanraotuibu unclickable issue by using SphereCollider centered at ping.004
This commit is contained in:
@@ -11,25 +11,39 @@ public class ChanraoTuibuHandler : MonoBehaviour
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
// 自动添加并适配包裹该物体网格的 BoxCollider,保证鼠标可以点到
|
||||
var mesh = GetMesh();
|
||||
if (mesh != null)
|
||||
// 尝试以 ping.004 为中心生成碰撞体,因为如果它是 SkinnedMeshRenderer,其自身原点可能在脚底
|
||||
var col = GetComponent<SphereCollider>();
|
||||
if (col == null) col = gameObject.AddComponent<SphereCollider>();
|
||||
|
||||
var ping004 = GameObject.Find("ping.004");
|
||||
if (ping004 == null)
|
||||
{
|
||||
var oldSph = GetComponent<SphereCollider>();
|
||||
if (oldSph != null) Destroy(oldSph);
|
||||
var mi = GameObject.Find("manInjury");
|
||||
if (mi != null)
|
||||
{
|
||||
var t = mi.transform.Find("ping.004");
|
||||
if (t != null) ping004 = t.gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
var col = GetComponent<BoxCollider>();
|
||||
if (col == null) col = gameObject.AddComponent<BoxCollider>();
|
||||
|
||||
col.center = mesh.bounds.center;
|
||||
col.size = mesh.bounds.size * 1.2f;
|
||||
if (ping004 != null)
|
||||
{
|
||||
// 将 ping.004 的世界坐标转换到当前物体的局部坐标系
|
||||
col.center = transform.InverseTransformPoint(ping004.transform.position);
|
||||
col.radius = 0.8f; // 弄大一点确保能点到
|
||||
col.enabled = true;
|
||||
Debug.Log($"[ChanraoTuibu] 碰撞体已定位到 ping.004 的位置: {col.center}");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GetComponent<Collider>() == null) gameObject.AddComponent<SphereCollider>().radius = 0.5f;
|
||||
col.center = Vector3.zero;
|
||||
col.radius = 0.8f;
|
||||
}
|
||||
|
||||
// 删除可能遗留的错误 BoxCollider
|
||||
var box = GetComponent<BoxCollider>();
|
||||
if (box != null) Destroy(box);
|
||||
|
||||
foreach (var r in GetComponentsInChildren<Renderer>(true)) r.enabled = false;
|
||||
CreateOutlineMesh();
|
||||
_isFinished = false;
|
||||
|
||||
Reference in New Issue
Block a user