When my player walks into this object, in this case a cube, I need it to simply attach to the player as if they’re carrying it.
This is what I’ve done so far.
var blockTransform : Transform;
var playerTransform : Transform;
function OnCollisionEnter (collision : Collision)
{
if(collision.transform.name == ("Player"))
{
blockTransform.transform.parent = playerTransform.transform;
}
}
you could have an empty game object as a child of the player and make a variable for it like holdSlot, then move your object to the spot like
blockTransform.transform.position = holdSlot.transform.position;
i would in most cases make picking up stuff happen from code on the player not from the thing you pick up though
I’ve got the solution now. To start with, the character controller is too large so no collision can be detected. Then, this code…
Put this on the player:
#pragma strict
var blockTransform : Transform;
var playerTransform : Transform;
var holdSlot : Transform;
function OnCollisionEnter (collision : Collision)
{
if(collision.transform.name == ("Block"));
{
blockTransform = collision.transform;
blockTransform.transform.parent = transform;
blockTransform.transform.position = holdSlot.transform.position;
}
}
The block gets translated to infront of the player, or where ever the holdSlot is placed automatically when the player touches it.