2D - locking the z axis using CharacterController.move

Here is my code so far.

Vector3 movement = new Vector3(Input.GetAxis (“Horizontal”) MoveSpeedTime.deltaTime, Input.GetAxis(“Jump”)JumpHeightTime.deltaTime,0);

cc.Move (movement);

cc.SimpleMove(Physics.gravity);

It works great, other than when I continuously run into a wall, my object starts to move in the z direction and eventually falls off the course. How do I lock him to position.z = 0?

I have tried this, but it doesn’t work.

 Vector3 pos = transform.position;
 pos.z = 0;
 transform.position = pos;

The character is still able to move in the z direction, and the gravity stops working correctly.

Thanks for your help.

Edit: I’ve also tried adding a rigid body, making it kinematic, switching off gravity, and freezing z position, it still slides into the z position while I move it into an angled cube

add a rigid body, that is kinemetatic, no gravity, freeze Z position