This script instantly transforms the object one unit in the Z-axis by pressing the up-arrow.
if (Input.GetKeyDown ("up")) {
transform.Translate(0, 0, 1);
}
How do I make this instead respond to the vertical-axis rather than the key, so that the movement controls won’t become difficult to use if the camera rotates? So pushing the up-arrow would always move the object forwards, if the camera attached to it was facing towards the Z-axis.
Ok so given a Camera looking at the scene with a unit on a grid. I think what you want to do is use the camera's forward direction to determine which way you want to go, but snap this direction so that the unit can only move on the grid.
//Get camera forward
Vector3 forward = Camera.main.transform.forward;
//x values are any value between -1.0f and 1.0f, the only valid values are -1, 0 and 1 so use Mathf.Round to force the values to be the closest integer.
float xSnap = Mathf.Round(forward.x);
//Don't bother with y as the unit is only moving on the x-z plane.
float zSnap = Mathf.Round(forward.z);
Vector3 snappedDirection = new Vector3(xSnap, 0.0f, zSnap);
//Finally translate the unit by this snappedDirection variable