How to make an object attach to or follow the player on collision?,How to make object attach/follow you after you hit it?

I have coded the collision stuff, so the game does detect if I hit it, but I don’t know how to make it stick/attach or follow the player! It would kinda work if you like picked it up! This is also a 3D game. Please help!

You could make it a child of your player, by using transform.parent.
In your collision detection, just set its parent to be the player (and maybe position) it like this

collision.gameObject.transform.parent = Player.transform;

This might not be the exact code you need but it should help to get you through.

Hey there,

i’d say there are 2 valid options:
First is like RetroGeek46 described it, even though i’d tend to put the “attachment” code in the OnCollisionEnter on the object that will stick to the player. Then it look a bit more like this:

public void OnCollisionEnter(Collision col)
	{
		if(col.transform.tag == "Player")
		{
			transform.parent = col.transform;
		}
	}

Your second option is to use a fixed joint. Attach it as component to your stickable object and set the connected body variable to the players rigidbody on impact (see here for details). This will fix it to the player like parenting but will give you some more options. Like breakforces for the bond, so that the player may shake of things that stuck to him if the forces between the rigidbodys get too large.
Disadvantage of the second option is that both the stickable object and the player both need a rigidbody attached. In case you use some other way of manouvering your player i’d advise to stick (no pun intended) to the first option :wink:

public void OnCollisionEnter(Collision col)
{
col.gameObject.transform.parent = Player.transform;

        if (col.transform.tag == "Player")
        {
            transform.parent = col.transform;
        }
    }

It gives me this error: “The name ‘identifier’ does not exist in the current context” though I have a object called Player.