C# 当子对象位于另一个对象下时,为什么对象比例不同?如何将对象缩放到相同的大小?

C# 当子对象位于另一个对象下时,为什么对象比例不同?如何将对象缩放到相同的大小?,c#,unity3d,C#,Unity3d,游戏开始时,我的对象名NAVI是一个板条箱的孩子。导航比例在X Y Z上设置为0.02 在X Y Z上,父板条箱_0_0尺寸比例设置为5 左侧是NAVI对象,右侧是板条箱0检验员比例设置 然后在游戏中一段时间后,我将NAVI对象移动为另一个对象的子对象: transform.parent = GameObject.Find("Navi Parent").transform;//rig_f_middle; transform.localPosition = GameObj

游戏开始时,我的对象名NAVI是一个板条箱的孩子。导航比例在X Y Z上设置为0.02

在X Y Z上,父板条箱_0_0尺寸比例设置为5 左侧是NAVI对象,右侧是板条箱0检验员比例设置

然后在游戏中一段时间后,我将NAVI对象移动为另一个对象的子对象:

transform.parent = GameObject.Find("Navi Parent").transform;//rig_f_middle;
 transform.localPosition = GameObject.Find("Navi Parent").transform.localPosition;
 transform.localRotation = Quaternion.identity;
 transform.localScale = new Vector3(0.001f, 0.001f, 0.001f);
我在这里将NAVI的比例设置为X Y Z上的0.001f,结果是现在NAVI在玩家手中,大小符合我的要求:

玩家比例在X Y Z上设置为1

这是NAVI小时候在玩家手中的屏幕截图:

rig_f_middle.02.R在X Y Z上的比例大小为0.9999999 Navi父对象的比例为X Y Z上的1

NAVI的比例现在是X Y Z上的0.001

问题是,如果我在NAVI是Carte的孩子时将其比例更改为0.001,它将太小,几乎看不到。即使设置为0.02,也太小了

我希望它在两个位置/情况下的大小,当它是点菜的子对象和玩家手的子对象时,是它在玩家手上看起来的大小

但比例并不相同。0.02取消点菜太小,但0.02比0.001大,当它在玩家手下时,0.001足够大

我如何知道或设置NAVI size的缩放比例,使其在两个位置上都与在玩家手中时相同?

尝试以下方法:

Vector3 targetScale; // The final world scale you want

Vector3 parentScale = navi.transform.parent.lossyScale;
navi.transform.localScale = new Vector3(
        targetScale.x / parentScale.x,
        targetScale.y / parentScale.y,
        targetScale.z / parentScale.z);
一个简单的替代方法是在世界空间中为人父母之前调整比例

transform.localScale = new Vector3(0.001f, 0.001f, 0.001f);

var parent = GameObject.Find("Navi Parent").transform;
// This will now keep the world scale you gave it before
// but change the "localScale" accordingly in order to basically
// do the calculation internally that was provided in the other answer
transform.parent = parent;
// I also doubt that you want to clone the localPosition here
// If the "Navi Parent" would have a certain offset against its parent 
// then you would be doubling it 
// -> You rather want to copy the absolute position
transform.position = parent.position;
transform.localRotation = Quaternion.identity;