How do you lock the first-person CharacterController to only walk along the x-axis?

I am making a 2.5D sidescroller and want to know if there is a way to take the built-in CharacterController and either: 1. Lock the movement to be only on the x-axis and y-axis(so that the character only moves left and right and may jump) or 2. Remove the keys W and S and UP and DOWN so that there can be no movement on the z-axis. I have read other “fixes” where people place invisible walls so that the character is restricted from moving along the z-axis, but surely there is a real fix to the problem.

You should modify the CharacterController movement script. If you’re using the standard First Person Controller, open the CharacterMotor.js script and find this instruction:

   	// Move our character!
	movement.collisionFlags = controller.Move (currentMovementOffset);

Add one instruction to zero the z movement before Move:

   	// Move our character!
	currentMovementOffset.z = 0; //<- zero movement in the z direction
	movement.collisionFlags = controller.Move (currentMovementOffset);

But be aware that CharacterMotor.js is a big monster that eats experienced programmers in the breakfast! Most modifications only result in undesired collateral effects, thus this one may also have weird consequences!

You would be interested in the “Constraints” section of the “Rigidbody” voice, in the Inspector. Look at here: Search for the voice “Freeze position”, and make sure that you’ve only the check on Z-axis, deactivating X- and Y- ones.
However, this works for physics, and for forces related to it: if you have a script that move the character using WASD, or arrows, movements along all these direction still remain valid. Reading at your question, it seems that you are using the “3d person controller”, that’s implemented by default on Unity. My advice is to make a script of yours, very easy indeed. Look at this:

var speed = 20.0;

function Update () {
   var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
   var y = Input.GetAxis("Vertical") * Time.deltaTime * speed;
   transform.Translate(x, y, 0);

The script above make the spaceship controllable only on X- and Y- axis, using arrows or WASD. Hope that it helps.