介紹
遇到的一些很奇怪,然后暫時(shí)還沒有解決的 Bug,有好心人知道怎么回事的話可留言或者發(fā)郵件到 zhangqrr@qq.com。
復(fù)現(xiàn)
在場景中新建三個(gè)同級的物體,分別如下所示:

Scene

Gameobject

Parent

Move
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject GoParent;
public GameObject GoMove;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.Equals(GoMove))
{
other.transform.SetParent(GoParent.transform,true);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.G))
{
GoMove.transform.SetParent(GoParent.transform, true);
}
}
}
可以看到腳本的內(nèi)容是把 Move 的父物體換成 parent,然后不改變其在世界坐標(biāo)的位置。兩種方式:
- 在 Update 里面通過按鍵來觸發(fā)
- 在 OnTriggerEnter 里面來觸發(fā)
運(yùn)行效果是在 Update 里面的符合預(yù)期,但是通過 OnTriggerEnter 觸發(fā)的,Move 就會改變在世界坐標(biāo)中的位置,而且很奇怪的是不是因?yàn)楸3至俗约旱木植孔鴺?biāo)不變而導(dǎo)致的世界坐標(biāo)位置變化,而是局部坐標(biāo)和世界坐標(biāo)位置都變了。