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. Dismiss Notice

Whats the best way to instantiate armor onto a character?

Discussion in 'Scripting' started by Durins-Bane, Jun 27, 2016.

  1. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    It seems to change with every object I spawn. Rather than have hundreds of objects on my char and then disabling them on start to enable the one I equip which im pretty sure is extremely inefficient, how do I properly spawn objects at the correct rotation. This is the last step before I pretty much have a basic rpg ready to start pumping some content into and making quests with rewards etc I made an ID based inventory/item db and can add the rotation to its info but it never seems to work

    Heres what I have so far https://gfycat.com/ThreadbareAdorableHoverfly
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'd create an empty GameObject as a child of the character at the correct position and rotation to act as a "bone" for the armor. Instantiate, set position and rotation equal to the "bone", and then make the bone the parent of the armor.
     
  3. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    This is how I have it setup already :/
    No matter which way I am facing though the object always instantiates looking the same way it seems

    Code (JavaScript):
    1. case 0:
    2.     if(switchingItems==true){}else{
    3.     if(head == 0){ switchingItems = true;
    4. //set the head id to selected slots id
    5.     head = inv.selectedSlotOB.GetComponent(slot).id;
    6.     eHead.GetComponent(Image).sprite = inv.selectedSlotOB.GetComponent(Image).sprite;
    7.     inv.selectedSlotOB.GetComponent(Image).sprite = defaultSlotSpr;
    8.     Debug.Log("EQUIPPED HEAD EMPTY");
    9.  
    10.     }else{
    11.     switchingItems = true;
    12.     var headBU : int = head;
    13.     head = inv.selectedSlotOB.GetComponent(slot).id;
    14.     eHead.GetComponent(Image).sprite = inv.selectedSlotOB.GetComponent(Image).sprite;
    15.     inv.selectedSlotOB.GetComponent(Image).sprite = defaultSlotSpr;
    16.     inv.equipPickUp=true;
    17.     inv.pickUpItem(headBU.ToString().ToString());
    18.     Debug.Log("EQUIPPED HEAD REPLACE");
    19.     }
    20.  
    21.     //Instantiate
    22.     var itemsObj : GameObject;
    23.     var ob : GameObject;
    24.     var childCount : int  = equipmentHead.transform.GetChildCount();
    25.     if(childCount!=0){
    26.      Destroy(equipmentHead.transform.GetChild(0).gameObject);
    27.      }
    28.  
    29.     db.findItem(inv.selectedSlotOB.GetComponent(slot).id.ToString());
    30.     //ob = db.findItem(inv.selectedSlotOB.GetComponent(slot).id.ToString());
    31.     //var equipRotation = Quaternion.Euler(db.rotX, db.rotY, db.rotZ);
    32.  
    33.     var itemsObj2 = Instantiate(itemToEquip, equipmentHead.transform.position, Quaternion.identity);
    34.    // itemsObj2.transform.rotation = Quaternion.identity;
    35.     itemsObj2.transform.SetParent(equipmentHead.transform);
    36.  
    37.      //itemsObj2.transform.eulerAngles = Vector3(db.rotX, db.rotY, db.rotZ);
    38.    // Transform.eulerAngles
    39.       //itemsObj2.GetComponent(setRotation).setRotationTo(db.rotX, db.rotY, db.rotZ);
    40.  
    41.     //itemsObj2.transform.rotation.y = transform.parent.localRotation.y;
    42.     //itemsObj2.transform.rotation.x = transform.parent.localRotation.x;
    43.     //itemsObj2.transform.rotation.z = transform.parent.localRotation.z;
    44.     //itemsObj2.transform.rotation = equipRotation;
    45.  
    46.     }
    47. break;
    Have tried all the commented out parts above but none of them seems to work :(
    So frustrating

    I have no idea how to go about this :/
    How do I get it into the right position/rotation
     
    Last edited: Jun 28, 2016
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're instantiating with a rotation of Quaternion.indentity when it should probably be equipmentHead.transform.rotation
     
    Durins-Bane likes this.
  5. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    This still wont work for some reason :(
    Its always off or when I try to set the rotation with another script attached to the object it seems to be random numbers compared to what I actually set it to
     
  6. listener

    listener

    Joined:
    Apr 2, 2012
    Posts:
    179
    Does your armor pivot orientation align with Unity3D orientation.
     
    Durins-Bane likes this.
  7. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
  8. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175