Attaching a game object to player when they touch. Also need it stay in posession of player when he moves.

Hello!

It is an icehockey game and im trying to make the puck attach to players stick when player touches the stick. Now the puck is attaching kinda but is acting wildly going all over the hockey field.
The puck should teleport to empty gameobject i created in front of the player and stay there as player moves.

public class Puck : MonoBehaviour
{

public GameObject holdSlot;
public GameObject puck;
Vector3 pos;

void Start()
{
    holdSlot = GameObject.Find("PuckHolder");
    pos = holdSlot.transform.position;
}

// Update is called once per frame
void Update()
{
    
}

public void OnCollisionEnter(Collision col)
{
    if (col.gameObject.name == "IceSlyder")
    {

        
       transform.parent = holdSlot.transform;
       transform.position = pos;
        //  HERE I NEED TO MAKE THE OBJECT ATTACH TO MY PLAYERS EMPTY GAME OBJECT "holdSlot"
        //  AND MAKE THE OBJECT A CHILD OF THE EMPTY GAME OBJECT SO IT STAYS IN PLAYERS POSESSION AS PLAYER MOVES

    }
}

}

First of all, Start() is called only once in the very beginning of the code runtime. When you assign some value to a field in Start() and you don’t reassign other values to the field later, it will have the same original value and will never be changed.
The second thing is: the position property of an object represents the object’s position related to the world pivot point at a certain particular moment during a runtime. Therefore, if you set pos = holdSlot.transform.position in Start() and you don’t update it later, then the pos will always represent that point where holdSlot has been located at the very beginning of the runtime, so the pos is actually useless in your case.


Also, when you attach an object to another object, you shouldn’t use a world position vector as a new position of the child (in most cases).
Transforms have two types of position properties:

  1. position - represents the offset relative to the world center pivot point;
  2. localPosition - represents the object’s offset relative to the object’s parent.
    When the object has no parents, its 'positionandlocalPositionare the same because the object is considered to be a child of the world scene in this case. When you set an object to be a child of another object (bymyObject.transform.parent = anotherObject.transform), the child's positionbecomes itslocalPosition(the engine doesn't change position properties in the parenting method), therefore, if the child (when it wasn't a child yet) had theposition = Vector3(1, 0, 0)`(1 meter to the right from the world center), then it will be located at the same offset from it’s new parent, its local position will be the same 1 meter to the right from the parent’s pivot point.
    And since the engine automatically adjusts all children positions when the parent is moved and/or rotated, the child will always be 1 meter to the right from the parent whenever you move/rotate the parent.

So, to properly put the child right at the parent object position you need to reset its localPosition to Vector3(0, 0, 0) right after the parenting method:

transform.parent = holdSlot.transform;
transform.localPosition = Vector3.zero;

Notice that you don’t even need the pos at all.