Search for align to ground/ slope code in the forum. The following code aligns the model to the ground:
result = c_trace(my.x,vector(my.x,my.y,my.z-2000,IGNORE_ME | IGNORE_PASSABLE);
vec_rotate(normal,vector(-my.pan,0,0));
my.tilt = -asin(normal.x);
my.roll = -asin(normal.y);
Instead you can replace tilt and roll with a vector and check their orientation.