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.