var pushPower = 1;
var pushLayers : LayerMask = -1;

// pointer to the player so we can get values from it quickly
private var player : PlayerControl;

function Start () {
	player = GetComponent (PlayerControl);
}

function OnControllerColliderHit (hit : ControllerColliderHit) {
	var body : Rigidbody = hit.collider.attachedRigidbody;

	if (body == null || body.isKinematic)
		return;

	var bodyLayerMask = 1 << body.gameObject.layer;
	if ((bodyLayerMask & pushLayers.value) == 0)
		return;
		
	if (hit.moveDirection.y < -0.3) 
		return;
	

	var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
	
	if (Mathf.Abs(pushDir.x) > Mathf.Abs(pushDir.z)) {
		pushDir = Vector3(pushDir.x, 0, 0) ;
	} else {
		pushDir = Vector3(0, 0, pushDir.z) ;
	}
	
	body.velocity = pushDir * pushPower * player.GetPushSpeed();
}

@script RequireComponent(PlayerControl)