- Home /
Help with a different approach to instantiating weapons?
http://answers.unity3d.com/questions/249940/making-instantiated-weapon-a-child-of-the-player.html
My problem is the same as in the above thread (essentially how to offset an instantiated weapon relative to the parent player), however, I was wondering if a different approach that might make it easier for me would be possible. I plan on having a relatively large number of weapons in my game, and it so i may have to have multiple different offset positions to correct the position and rotation of each. For this reason I am looking for an alternative to using empty gameobjects, and creating one for each, or at least many, different weapon. What I thought would work was setting the position and rotation variables on the actual weapon prefab, then passing them into the weapon equip script like this:
Transform item =(Transform)Instantiate(itemPrefab, transform.position, transform.rotation);
//attach weapon to player
item.parent = transform;
Vector3 position = new Vector3(itemPrefab.transform.position.x, itemPrefab.transform.position.y, itemPrefab.transform.position.z);
Quaternion rotation = transform.rotation;
rotation.eulerAngles = new Vector3(rotation.eulerAngles.x, rotation.eulerAngles.y + itemPrefab.transform.rotation.y, rotation.eulerAngles.z);
item.transform.position += position;
item.transform.rotation = Quaternion.Euler(0,itemPrefab.transform.rotation.y, 0);
However, the offset position does not take into account the rotation of the player, so if I were to say offset the weapon by 1.0f forward, it would spawn at the coordinate of the player + 1.0f, which could be in front of the player, to the left, behind, etc... depending on where the player is looking when the weapon is instatiated.
Is there a way around this? Would it be easier and simpler to just use empty gameobjects? One of my primary concerns is that if I parent like 30 different gameobjects to the player, anticipating every possible location I would want a weapon to be, that it might eat up preformance?
Thanks for any help with this question! I'll post more code if this is not enough.
Answer by VesuvianPrime · Dec 12, 2014 at 03:44 AM
You're very close to getting this working, you just need to set the localPosition and localRotation of the weapon transform instead of the world position and rotation.
Thanks, it works perfectly!
If anyone has the same problem in the future, here is my code:
Transform item =(Transform)Instantiate(itemPrefab, itemPosition.transform.position, itemPosition.transform.rotation);
item.parent = itemPosition.transform;
item.transform.localPosition += itemPrefab.position;
item.transform.localRotation = itemPrefab.rotation;
"itemPosition" is an empty gameObject at the position of the camera. I knew I said I didn't want to use gameObjects, but with this method I only have to use one.
Answer by Exploder1010 · Dec 12, 2014 at 09:59 AM
Thanks, it works perfectly!
If anyone has the same problem in the future, here is my code:
Transform item =(Transform)Instantiate(itemPrefab, itemPosition.transform.position, itemPosition.transform.rotation);
item.parent = itemPosition.transform;
item.transform.localPosition += itemPrefab.position;
item.transform.localRotation = itemPrefab.rotation;
"itemPosition" is an empty gameObject at the position of the camera, and on the equipped weapons layer (so that I can set a culling mask to a secondary camera to prevent the weapon from clipping through walls). I knew I said I didn't want to use gameObjects, but with this method I only have to use one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613173842im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Instantiating an object via its center (as opposed to its pivot point) 2 Answers
Stop Children of Instantiated Parent From Offsetting After Changing Position 0 Answers
How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers
Instantiate New Gun 1 Answer