Instantiating a prefab using its position values?

I’m having a nightmare of a time solving this issue.

I have a character model, with a hand bone. In my game I will need to attach variety of different weapons to this hand bone, so I need a method of making sure the weapons attach properly, facing the right direction/rotation when they stick in the hand.

I’ve started by trying to get my first weapon to equip properly, however no matter what trick I try, the weapon will not listen when being instantiated.

So far what I’ve done is put the weapon in the models hand attached to the hand bone as its child, and while its there I set the rotation and position of the sword to make the sword fit perfectly in the hand, then saved those rotation and position values in the prefab, and then saved the values again in 2 variables on the item.

Instance 1:

       EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemToEquip.ObjectSlug), playerHand.transform.position, playerHand.transform.rotation);

        EquippedWeapon.transform.SetParent(playerHand.transform);

This properly equips the weapon to the hand as its child, but it is offset a bit and hanging off of the model, not using the prefabs rotations, so I then decided to put the prefabs rotation and position values directly into variables on the weapons interface,

    public Quaternion defaultrotation{ get; set; }
    public Vector3 defaultposition{ get ; set; }

    public void Awake()
    {
        defaultposition = new Vector3(-2f, 0.082f, 0.131f);
        defaultrotation = Quaternion.Euler(-52.512f, 65.29501f, -63.275f); }

And then I call on these values after the parent is set for the weapon after it has bee instantiated:

        EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemToEquip.ObjectSlug), playerHand.transform.position, playerHand.transform.rotation);
        EquippedWeapon.transform.SetParent(playerHand.transform);
        EquippedWeapon.transform.rotation = EquippedWeapon.GetComponent<IWeapon> ().defaultrotation;
        EquippedWeapon.transform.position = EquippedWeapon.GetComponent<IWeapon> ().defaultposition;

Millions of games attach different weapon models to a models hands with perfect rotation and position, and everything I’ve found online simply tells me to attach it to the bone, which I’m doing. What do I need t

but this is sending the sword into Africa. I am a bit new to unity, and there’s clearly some sort of interaction I’m messing up here, but I’ve spent the better part of an evening and I can’t solve this.

Millions of games attach different weapon models to a models hands with perfect rotation and position, and everything I’ve found online simply tells me to attach it to the bone, which I’m doing. What do I need to do to get the sword to equip to the hand parent in the correct position? is it possible with script or do I need to change something with the sword? plx halp

Try using localPosition and localRotation instead of position and rotation (which work in world space (or “absolute” space if that helps you think about positions in general).

When you attach a weapon to the hand, you want the weapon to inherit the hand’s position and rotation. So you probably don’t even need to set the local position/rotation after attaching it to the hand, unless you wanted to add some sort of a local offset (just set localPosition to Vector3.zero and localRotation to Quaternion.identity after attaching to the hand).

1 Like

That worked, 100%, I knew I was missing something, I had no idea about local and world positions and the differences. Still really new to Unity. I love you, you’re my hero today! Can’t tell you how long I spent trying to get this to work.

1 Like