Accessing objects position from another script

I’m making a game with both a ball and a player. So far I made a sphere for the ball and a cube (models will be made later) for the player. I attached a movement script to the player so that it can go in all directions, but I want him to be able to pick up the ball when he runs into it. To do this, I’m assuming that in the ball script, within a collision function, I would have to change it’s position to the position of the player. So I’m wondering: what is the correct way of accessing those coordinates of the player from the ball script?

Thanks

1 Answer

1

Assuming an ‘OnCollision’ type script,

void OnCollision(Collision hit)
{
    if(Collision.gameObject.tag == "Player")
    {
        transform.position = Collision.transform.position;
        transform.parent = Collision.transform;
    }
}

Of course, you probably don’t want the ball to snap right to the centre of the player’s body, but this script should get you started.