- Home /
inserting random gun into player/enemies hands
Hello,
I am recently new to unity, and I am trying to slowly learn everything I'll need to know to make my game work. so for I have gotten pretty much everything, except for 1 thing.
I want to have the enemies guns be randomly generated, using code to randomly attach the components to the guns ( adding extras such as scopes and such ) and to randomly select which gun each unit will actually use.
I can figure out the coding of mostly everything else, but im not sure how to actually add a weapon in this fashion, without manually creating and pre-parenting each gun. ( there will be about 500 iterations of the various guns.) im really confused, since i cant seem to make an object parent under a prefab ( which is the issue im currently having. )
i thought about adding an empty game object under the hand joint, but how can i code the gun to instantiate in that, since wouldnt that be still in a prefab?
any ideas on this?
Answer by aldonaletto · Jul 04, 2011 at 02:26 AM
If you want to have several different weapons and add to each one a variable number of accessories, I think the best alternative is to have all accessories already added to each weapon prefab, and enable or disable itens using SetActiveRecursively - it can enable or disable the game object and all its childs. You should instantiate the prefab weapon of the selected type, then enable the selected accessories. The FPS tutorial uses this method to select the weapon: all weapons are childed to the player, but only one of them is active at a time - the others remain invisible and inactive (in your case the accessories would be acivated/deactivated). The code below is an example: it sweeps all accessories childed to the object and randomly enable about 25% of them:
for (var i=0;i<transform.childCount;i++){
var activate = (Random.Range(0,100)<25);
transform.GetChild(i).gameObject.SetActiveRecursively(activate);
}
EDITED: Another alternative is to simply delete the unwanted accessories right after the weapon is instantiated. You can instantiate one single weapon, eliminate the unwanted accessories and instantiate the others from this modified one:
firstWeapon = Instantiate(weapon23, position, rotation);
for (var i=0;i<firstWeapon.transform.childCount;i++){
if (Random.Range(0,100)>25){
Destroy(firstWeapon.transform.GetChild(i).gameObject);
}
}
// then instantiate copies of firstWeapon
weapon = Instantiate(firstWeapon, otherPos, otherRot);
Quoting the original post:
"...without manually creating and pre-parenting each gun. ( there will be about 500 iterations of the various guns.) "
I think @aravynn means "without manually creating 500 different prefabs" - the weapons must be created manually at design time anyway. Ins$$anonymous$$d of creating one prefab for each possible combination of each weapon, my idea is to have the weapon prefabs complete with all accessories, and when instantiated disable the unwanted ones by script. If he needs 10 equal weapons with 2 or 3 specific accessories, for instance, he can instantiate the complete weapon prefab and eli$$anonymous$$ate the undesired features. Another similar approach - and maybe more efficient - could be simply to destroy the unwanted children (it sounds so cruel!)
Answer by SilverTabby · Jul 04, 2011 at 03:13 AM
A quick glance at the Transform documentation tells me that if you are trying to parent one object to another from inside a script, you can just say transfrom.parent = target;
and that should parent your gun/prefab/whatever to your hand/leg/whatever
Maybe try
createdObject.SendMessage("setParent", targetGameObject);
and then on a script in your prefab,
function setParent(target : Transform){this.transform.parent = target;}
Answer by Waz · Jul 04, 2011 at 01:58 PM
Once your player is instantiated, it is not "in a prefab". You can, from code, Instantiate other objects and parent them into place in the hands, and for that matter parent scopes onto rifles. As SilverTabby says, just set the parent:
createdObject.transform.parent = hand;
You probably then want to set the localPosition to zero and the localRotation to identity.
Unless you have a very small number of guns, pre-parenting will be hard to maintain (and with 500, I can see why you want to avoid it!)
I think the problem with adding accessories is the reference: any accessory prefab should be carefully adjusted in order to have the same pivot as the weapon, so it could be instantiated at the same position and rotation, and then childed to the weapon. That's why I think eli$$anonymous$$ating unwanted already installed accessories is easier than adding the desired ones.
Your answer
Follow this Question
Related Questions
How to instantiate prefabs between 2 objects like a path 0 Answers
Procedural generation of prefab objects 1 Answer
Instantiate New Gun 1 Answer
Network.Instantiate a non-prefab? 1 Answer
Unity not Instantiating Prefabs Properly 0 Answers