简体   繁体   English

将加载的预制件附加到对象的Unity 3D奇怪行为

[英]Unity 3D Strange Behaviour Attaching Loaded Prefab to Object

Hey Everyone could use some advice. 大家都可以使用一些建议。 On running my program I'm loading a prefab and attaching it to a bone of a character object. 在运行程序时,我正在加载预制件并将其附加到角色对象的骨骼上。

The character is called "MrPresident" and it has an attach point called "genSuit_AttachHandRight". 该角色称为“ MrPresident”,并且具有名为“ genSuit_AttachHandRight”的连接点。 Through another script I call this function to load a prefab called 'basicBriefcase', attach it to this attach point, and attempt to set its position to (0, 0, 0) relative to the attach point.: 通过另一个脚本,我调用此函数以加载名为“ basicBriefcase”的预制件,将其附加到此附加点,并尝试将其位置设置为相对于附加点的(0,0,0)。

public void Equip(string mode)
{
    // on startup load basic equipment
    if ( mode == "Initialize" )
    {
        // right hand equipment
        string modelRightAttachPoint = championProperties.modelRightAttachPoint;
        string defaultRightHandType = championProperties.defaultRightHandType;
        string defaultRightHandObject = championProperties.defaultRightHandObject;
        if ( defaultRightHandType == "custom")
        {
            string championName = this.name;
            GameObject equipment = Instantiate(Resources.Load("Equipment/" + championName + "/" + defaultRightHandObject, typeof(GameObject))) as GameObject;
            equipment.name = defaultRightHandObject; // gets rid of the (Clone)

            equipment.transform.parent = GameObject.Find(modelRightAttachPoint).transform;
            equipment.transform.position = Vector3.zero;
        }
    }
}

The prefab is loading in the Hierarchy and appears to be attached to the attach point when I press play, but its nowhere to bee seen in the Game view. 预制件正在加载到层次结构中,并且在我按播放时似乎已附加到附加点,但是在“游戏”视图中看不到它的蜜蜂。 When I try debugging by running this: 当我尝试通过运行以下命令进行调试时:

public void Update()
{
    print(GameObject.Find("basicBriefcase").transform.position);
}

The value is flying all over the place. 价值四处飞扬。 The first line of output is (0, 0, 0) on the first Update, but the position after that its changing every cycle through update. 在第一个Update上,输出的第一行是(0,0,0),但之后的位置在更新过程中每个周期都会改变。 The character its attached to is animated but its just sitting in place, and I would think the position relative to the attach point should remain constant at what i set it. 它附加到的角色是动画的,但只是就位,我认为相对于附加点的位置应保持在我设置的位置不变。 If I remove the line 如果我删除线

equipment.transform.parent = GameObject.Find(modelRightAttachPoint).transform;

Then the position stays at (0, 0, 0), but because i havent attached to the approprite parent (the attach point) the position is obviously wrong. 然后位置保持在(0,0,0),但是由于我还没有附加到Approprite父对象(附加点),因此该位置显然是错误的。 Am I missing something here? 我在这里想念什么吗? Thanks for your help. 谢谢你的帮助。

EDIT 编辑

I discovered that i should be using 我发现我应该使用

equipment.transform.localPosition = Vector3.zero;

instead of .position but now I'm having a whole new problem. 而不是.position,但是现在我遇到了一个全新的问题。 Where before the equipment was attaching ok to the parent, now it just dissapears when I try to set the parent. 在设备可以正常连接到父级之前的位置,现在当我尝试设置父级时,它消失了。

If i execute this statement 如果我执行此语句

GameObject equipment = Instantiate(Resources.Load(startPath + defaultRightHandObject, typeof(GameObject))) as GameObject;

The object appears in the hierarchy with no parent. 该对象显示在层次结构中,没有父级。 But if I try to do: 但是,如果我尝试这样做:

Transform trans = GameObject.Find("MrPresidentAttachRightHand").transform;     
equipment.transform.SetParent(trans, true);

The object just dissapears. 该对象消失了。 Its nowhere to be found, its not attached to the parent it should be attached to. 它无处可寻,它没有附加到应该附加的父目录。 I've tried using .parent and .SetParent. 我试过使用.parent和.SetParent。 I've tried reloading the prefab it is supposed to attach to. 我已经尝试重新加载它应该附加的预制件。 If i try to set the parent to something like Camera.main.transform, it works. 如果我尝试将父级设置为Camera.main.transform之类的格式,则可以正常工作。 If i set it to the highest parent in the prefab i'm trying to attach it to, it works. 如果我将其设置为我要附加到预制件中的最高父级,则它可以工作。 But if i try to attach it to the attach point, it dissapears. 但是,如果我尝试将其附加到附加点,它将消失。 What am I doing wrong? 我究竟做错了什么?

Just don't use transform.parent to set the parent of a GameObject. 只是不要使用transform.parent设置GameObject的父代。 Use the transform.SetParent function which let's you specify if the object should be place relative to the parent. 使用transform.SetParent函数,让您指定对象是否应相对于父对象放置。

That should be: 应该是:

Transform trans = GameObject.Find(modelRightAttachPoint).transform;
equipment.transform.SetParent(trans, true);

If you still have issue, pass false to it: 如果仍然有问题,请将false传递给它:

Transform trans = GameObject.Find(modelRightAttachPoint).transform;
equipment.transform.SetParent(trans, false);

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

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