0
\$\begingroup\$

I've seen that the bones are Transforms. And i've tried copying many examples. The armature is the standard MakeHuman one and is unchanged. It imported to Unity fine and the mocap animations work perfectly. What I need to do is make the hand a parent of my gun .

Here is what I have currently (I have tried many similar permutations):

    Transform handR;
public GameObject gunPrefab;
GameObject gun;

private void Start()
{
    rb = GetComponent<Rigidbody>();
    playerAnims = GetComponentInChildren<PlayerAnimations>();
    absoluteGround = rb.transform.position.y;
    distToGround = GetComponent<Collider>().bounds.extents.y;
    handR = transform.Find("hand_r");
    Debug.Log("handR really is: " + handR);

    gun = GameObject.Instantiate(gunPrefab);
    gun.transform.SetParent(handR);
    //gun.transform.position = handR.position;
    gun.transform.rotation = Quaternion.identity; 
}

picture showing imported bones structure

The end result of this: The gun will spawn but isnt parented to anything. And if I try to set its position using handR.position then i get null exception. Pic above shows the image where I see its called "hand_r"

One more thing of note: The bones shown in the image above , do NOT appear in the hierarchy. I have on occasion imported models with the bones included whose bone names all appear in the hierarchy - could this be the reason Unity is finding "hand_r"??? and if so whats the best way to fix that (Please done say remove the armature and make my own in Blender !! :O )

PS: I have since tried it with a new model, made in Blender with a Rigify rig. The code part "handR = transform.Find("hand_R"); -yes the rigify rig names it with capital "R". Still doesnt work, BUT THERE IS A WORKAROUND I FOUND... if I make the Transform handR public and drag it into the Inspector it all works fine! (And yes the Rigify skeleton also shows up in hierarchy!) (So if you are reading this and struggling too, DONT use the MakeHuman skeleton, use one from Blender Rigify.)

But I'm still very confused why the "Find" command doesnt find the bone

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Have you tried reading about transform.find on the scripting Api?

"public Transform.Find(string name)

Finds a child by name and returns it.

IF NO CHILD WITH NAME can be found, null is returned. If name contains a '/' character it will access the Transform in the hierarchy like a path name.

Note: Find does not perform a recursive descend down a Transform hierarchy."

Here is an example of its usage:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
public GameObject player;
public GameObject gun;
public Transform ammo;

//Invoked when a button is clicked.
public void Example()
{
    //Finds and assigns the child of the player named "Gun".
    gun = player.transform.Find("Gun").gameObject;

    //If the child was found.
    if (gun != null)
    {
        //Find the child named "ammo" of the gameobject "magazine" (magazine is a child of "gun").
        ammo = gun.transform.Find("magazine/ammo");
    }
    else Debug.Log("No child with the name 'Gun' attached to the player");
    }
}

It's not working in your case because of its wrong usage inside the script.

\$\endgroup\$
1
  • \$\begingroup\$ hi there, thanks i did see this, but i couldnt understand why it isnt there. The child transform "hand_r" was there existing and being used as part of the animations clips. Luckily a change of armature (now I am using Blender Rigify instead of the MakeHuman json game skeleton) and it appears in hierarchy and I can just drag it in Inspector. I guess I needed something like "clavicle_r/arm_r/hand_r etc etc" for it to Find it, but I tried many strings and none of them seemed to work \$\endgroup\$ Nov 26, 2017 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .