Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Wired thinks with transformations and weapons attached to a npc hand

Discussion in 'Scripting' started by juglarx, Sep 18, 2018.

  1. juglarx

    juglarx

    Joined:
    Dec 27, 2014
    Posts:
    60
    Hi all I have wired effect I have an NPC and I would like to attach a weapon to his hand... so far so good but when I script the NPC i used this code.


    Code (CSharp):
    1.      knife.transform.parent = transform.Find("Root_jnt/Hips_jnt/Body_jnt/Spine_jnt/UpperArm_Right_jnt/LowerArm_Right_jnt/Hand_Right_jnt").transform;
    2.         knife.transform.localPosition = transform.Find("Root_jnt/Hips_jnt/Body_jnt/Spine_jnt/UpperArm_Right_jnt/LowerArm_Right_jnt/Hand_Right_jnt").position;
    3.         knife.transform.localRotation = transform.Find("Root_jnt/Hips_jnt/Body_jnt/Spine_jnt/UpperArm_Right_jnt/LowerArm_Right_jnt/Hand_Right_jnt").rotation;
    When a use the line
    Code (CSharp):
    1. knife.transform.parent = transform.Find("Root_jnt/Hips_jnt/Body_jnt/Spine_jnt/UpperArm_Right_jnt/LowerArm_Right_jnt/Hand_Right_jnt").transform;
    I notice that in the editor i have the weapon in a gray color, as disable(?) or something and i dont see the item

    if i put the following code on the update method:

    Code (CSharp):
    1.      knife.transform.localPosition = transform.Find("Root_jnt/Hips_jnt/Body_jnt/Spine_jnt/UpperArm_Right_jnt/LowerArm_Right_jnt/Hand_Right_jnt").position;
    2.         knife.transform.localRotation = transform.Find("Root_jnt/Hips_jnt/Body_jnt/Spine_jnt/UpperArm_Right_jnt/LowerArm_Right_jnt/Hand_Right_jnt").rotation;
    the weapon is attached to the NPC But.... it´s pointed wrongly

    anyone can help me ?

    thanks in advance
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Well first and foremost, is there no way to link the proper transform in the inspector so that we don't need to do a very long Find? That would simplify things and make sure everything works every time.

    As for the incorrect rotations, you want to make sure the knife's origin point is in the correct position and rotation so that when its parented to something, it will follow along in the manner you like. If the model/mesh isn't already doing so, you can place it inside of an empty GameObject and move it around inside there first. You most likely want the handle to be the origin point. Then you'd simply parent the object and set the localPosition and localRotation to Vector3.zero and Quaternion.identity respectively.
     
  3. juglarx

    juglarx

    Joined:
    Dec 27, 2014
    Posts:
    60
    thanks groZZleR i´ll try
     
  4. juglarx

    juglarx

    Joined:
    Dec 27, 2014
    Posts:
    60
    nothing works... :(
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What aspect(s) don't work? Can you provide some screenshots?

    We'll figure it out.
     
  6. juglarx

    juglarx

    Joined:
    Dec 27, 2014
    Posts:
    60
    Hi, i solved, stupid error... the structure inside the model was deactivated.... mainly i went to the model step on the root and activate it from the inspector
    cheers