Chield gameobject with local postion Vector3.zero, doesn't have same position as parent object

huangapple go评论96阅读模式
英文:

Chield gameobject with local postion Vector3.zero, doesn't have same position as parent object

问题

我有以下代码:

relationHint = Instantiate(prefabRelationHint) as GameObject;
relationHint.transform.SetParent(imageObject.transform, false);
relationHint.transform.localPosition = Vector3.zero;

目标是在与imageObject(红色笑脸)相同的位置生成relationHint(带有文本的白色方块)。在检查器中一切看起来都正常,因为relationHint被创建为imageObject的子物体,位置为0,0,0,但在相机中它的位置与imageObject不同。

你做错了什么?

英文:

I have following code:

relationHint = Instantiate(prefabRelationHint) as GameObject;
relationHint.transform.SetParent(imageObject.transform, false);
relationHint.transform.localPosition = Vector3.zero;

Goal is to spawn relationHint (white square with text) at same position as imageObject (Red smile face). Everything look OK in inspector, since relationHint gets created as chield of imageObject and has position 0,0,0, but on the camera it doesn't have same position as imageObject.

Chield gameobject with local postion Vector3.zero, doesn't have same position as parent object
Chield gameobject with local postion Vector3.zero, doesn't have same position as parent object

What am I doing wrong?

答案1

得分: 1

将HitRelation的枢轴设置为x:0.5,y:0.5。这是中心值。
当你将它设置为(2, 2)时,你在说游戏对象的中心位于(2, 2)点。而当你将localPosition设置为(0, 0, 0)时,这一点位于中心。

英文:

You need to set HitRelation's pivot to x:0.5, y:0.5. This is the center value.

When you make it (2, 2) you're saying that game objects center is in (2, 2) point. And this point is centered when you set localPosition to (0, 0, 0).

huangapple
  • 本文由 发表于 2023年4月10日 20:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定