- Home /
How to manage position of player sub objects
I'm making game in which player could switch between diferent types of weapons.
But, each weapon requares its own(weapon) position, hands position and rotation angle for all of them. The only solution I could imagine, is storing positions and rotations for all types of weapons.
Like this:
Vector3 leftHandPos_0 = new Vector3 (-0.38f, -0.72f, 2f);
Vector3 rightHandPos_0 = new Vector3 (0.29f, -0.66f, 1f);
Vector3 gunPos_0 = Vector3.zero;
Vector3 shootingPos_0 = new Vector3 (-0.02f, 0.66f, 0f);
Vector3 leftHandPos_1 = new Vector3 (-0.38f, -0.72f, 2f);
Vector3 rightHandPos_1 = new Vector3 (0.29f, -0.66f, 1f);
Vector3 gunPos_1 = Vector3.zero;
Vector3 shootingPos_1 = new Vector3 (-0.02f, 0.66f, 0f);
Cant quite get how solve this problem in more correct way.
You could also create empty gameobjects and parent them under your player. Name the left hand, right hand etc , and attach the weapons to them. You could then use some variable of the weapon, like its collider.size or collider.center to place them so they look ok
Answer by Baste · Feb 23, 2015 at 04:08 PM
If you're working with models from a 3D program that you or somebody you're working with is making (IE. not something from the asset store), a great trick is to add marker bones that signify the relative position and rotation of the weapon. Then you just set that bone as the weapon's parent, with a local position and rotation of Vector3.zero.
Another way to solve this is to add all of the weapons to the model, and disable the renderer on the ones that are not currently equipped. That'll allow you to position them manually.
In general, whenever you're writing something like this:
Vector3 leftHandPos_0 = new Vector3 (-0.38f, -0.72f, 2f);
You're probably doing something wrong. If you change the weapon model, your code wont work anymore
Can also do the same thing in Unity by adding empty transforms (which is what the marker bones become, anyway.) But I think bone/empty "mount points" made with the model, in the modeling program, is a pretty standard thing. Like if you ask the modeler, they might cut you off with "sure, sure -- wondered why you didn't ask before."