简体   繁体   English

实例化为什么Unity Prefab值错误?

[英]Why are Unity Prefab values wrong when instantiated?

I have a C# script on a character that holds a reference to a Prefab. 我有一个C#脚本,其中包含对Prefab的引用。

During initialization, the script runs : 在初始化期间,脚本运行:

weaponSlot = Instantiate(weaponPrefab) as Transform;

and sets 和集

weaponSlot.parent = rightHand;

the prefab contains scaling information for the weapon, as well as some small rotation and position offsets for it to look correct. 预制件包含武器的缩放比例信息以及一些小的旋转和位置偏移,以使其看起来正确。

When the game is run, the weapon's actual position is offset from the rightHand by a massive amount, although the rotation is preserved. 当游戏运行时,武器的实际位置会从右手偏移大量,尽管会保持旋转。 The scaling is also a bit off, smaller than the prefab-ed size by roughly 40%. 缩放比例也略有降低,比预制尺寸小约40%。

Any insight on why this is happening, or even hints on what to check would be appreciated! 对于为什么发生这种情况的任何见解,甚至对检查内容的提示都将不胜感激!

Make sure to wrap any models in an empty game object. 确保将任何模型包装在一个空的游戏对象中。 The size, position, and orientation need to be correct under the root gameobject. 根游戏对象下的大小,位置和方向必须正确。 When you instantiate a gameobject under a parent you need to be sure to zero-out the localPosition, and localEulerAngles (set them = Vector3.zero). 当实例化父对象下的游戏对象时,需要确保将localPosition和localEulerAngles归零(将它们设置为Vector3.zero)。 You need to also set the localScale = Vector3.one. 您还需要设置localScale = Vector3.one。

It should look like this in the project: 在项目中应如下所示:

Prefab (zero position, zero rotation, one scale) 预制件(零位置,零旋转,一个刻度)
->Model (correct scaling, rotation, and position) ->模型(正确的缩放比例,旋转和位置)

Then you parent it. 然后您将其作为父项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM