介绍
遇到的一些很奇怪,然后暂时还没有解决的 Bug,有好心人知道怎么回事的话可留言或者发邮件到 zhangqrr@qq.com
。
复现
在场景中新建三个同级的物体,分别如下所示:




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);
}
}
}
可以看到脚本的内容是把 Move 的父物体换成 parent,然后不改变其在世界坐标的位置。两种方式:
- 在 Update 里面通过按键来触发
- 在 OnTriggerEnter 里面来触发
运行效果是在 Update 里面的符合预期,但是通过 OnTriggerEnter 触发的,Move 就会改变在世界坐标中的位置,而且很奇怪的是不是因为保持了自己的局部坐标不变而导致的世界坐标位置变化,而是局部坐标和世界坐标位置都变了。
网友评论