Freeze movement of game object to one direction(Horizontal)

I’m using following script to move gameobject when player push it, But i want that my gameobject should only move in one (horizontal) direction, as now it can move to any direction even it bounces if player jump while pushing it.

JS:
var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit) {
var body : Rigidbody = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if (hit.moveDirection.y < -0.3)
return;
var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}

In the other object’s rigid body, check freeze z and y.

The trick only works for rigidbodies, only affects velocity and force (it won’t stop you from changing position z and y directly) and only woks for global x,y z axises (you can’t turn it then lock it the new, diagonal, x.)

I actually had stumbled across something for my last project relating to this.

It should work with anything, not only rigidbodies.

var lastLegalLoc : Vector3;
var zConstraint : float;

function Start(){

	lastLegalLoc = transform.position;
	zConstraint = transform.position.z;

}

function Update(){

	if(transform.position.z == zConstraint) lastLegalLoc = transform.position;
	else transform.position = lastLegalLoc; 

}

Why Dont you freeze the Rotation From Inspector?