For the trace code you need something like this (not tested):
Code:
function attachToGround()
{
	vec_set(target,my.x);
	target.z -= 1000;
	
	result = c_trace(my.x,target,IGNORE_PASSABLE | IGNORE_ME | USE_BOX);
	
	if(result + my.min_z > 0)
	{
		my.z -= result + my.min_z;
	}
}