- Home /
Weapon ("Snapping") Socketing FPS
Hi,
I am making a system where by a player in a FPS has a socket parented to their capsule collider which indicates where any weapons this player picks up need to be located.
It then needs to, upon acquisition, snap the weapons mesh onto that socket. However the catch is there is an offset from the meshes transform which is in the form of a socket on each weapon. It is the two sockets (player and weapon) that need to be aligned but the meshes transform that needs to be moved.
This means I need to calculate the meshes new Vector3 position (the rotation and scale are taken care of) based on the location it has to go (the players socket) the location its snapping onto (the weapons socket) and the location of what is actually displayed (the weapon mesh).
I am pretty sure this comes down to differences between local and world space. Does a childed game objects position represent an offset from the parents position?
If so could I just align the meshes transform and the player socket and then create a new Vector3 equal to the meshes position - the sockets position. When my weapon is being attached it will be existing in world space and so the meshes transform will not be childed to anything else.
TLDR: Is a games objects childs transforms position a representation of an offset from the parents position?
Answer by Julien-Lynge · Mar 18, 2013 at 11:45 PM
TLDR answer:
Childing a game object to another means that it's transform is relative to the local coordinate space of the parent. In other words, the parent exists at (0,0,0), zero rotation, and (1,1,1) scale, and the child is based in that coordinate system. So yes, a child's transform position is an offset from the parents position, according to the parent's rotation and scale.
so say the player socket was at O = (0,0,0) and the mesh was at some arbitrary point say P = (10,3,6) the weapon socket is childed to the mesh and has a position of $$anonymous$$ = (1,1,1) which in world space is (11,4,7) (?). That means since I want the two sockets to align I need to set the position of P to be O - $$anonymous$$ = (-1,-1,-1)? Then $$anonymous$$ which is local to P would be (0,0,0) same as O.
This is true for transform.localPosition, localScale, and localRotation only. The world variations are .position, .lossyScale (which cannot be set), and .rotation
You can set weapon.position = socket.position, which is the same as (weapon.parent = socket.parent; weapon.localPosition = socket.localPosition)
Your answer
![](https://koobas.hobune.stream/wayback/20220613094653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distance on fps 3 Answers
Lens-Flare Muzzleflash 0 Answers
Enemy death help 1 Answer
Class member Vector3 based on local transform 1 Answer
Health Script 1 Answer