Why Is My Movement Script Only Partially Functional?

In my movement script, coded in C#, the horizontal axis controls are fully recognized and their required actions carried out. However, my vertical axis controls are either ignored or not carried out properly.

Specifically, my forward key (inputManager.isInput[0]) does absolutely nothing, while my backward key (inputManager.isInput[1]) performs the jump function of (inputManager.isInput[4]).

This movement script was created by adapting the movement script on the Unity Scripting Reference, that script was found in the page entitled ‘CharacterController.Move’.

My adapted script is as follows:

using UnityEngine;
using System.Collections;

public class Movement_V : MonoBehaviour {

	public float speed = 6.0F;
	public float jumpSpeed = 8.0F;
	public float gravity;

	private float horizontal;
	private float vertical;

	private Vector3 moveDirection = Vector3.zero;

	public custom_inputs inputManager;
	public Menu_Control_CS menuControl;

	void Update()
	{
		CharacterController controller = GetComponent<CharacterController>();

		if(menuControl.mainMenuOn == true)
		{
			gravity = 0;
		}
		else
		{
			gravity = 20;
		}

        if(inputManager.isInput[0])
		{
			vertical = -1;
		}
		else if (vertical < 0)
		{
			vertical += 1;
		}

		if(inputManager.isInput[1])
		{
			vertical = 1;
		}
		else if (vertical > 0)
		{
			vertical -= 1;
		}

		if(inputManager.isInput[2])
		{
			horizontal = -1;
		}
		else if (horizontal < 0)
		{
			horizontal += 1;
		}

		if(inputManager.isInput[3])
		{
			horizontal = 1;
		}
		else if (horizontal > 0)
		{
			horizontal -= 1;
		}

		if (controller.isGrounded)
		{
			moveDirection = new Vector3(horizontal, 0, vertical);
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;

			if (inputManager.isInputUp[4])
			{
				moveDirection.y = jumpSpeed;
			}
			
		}

		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);
	}
}

The input manager array:

inputManager.isInput[4]

is used to find when certain keys being pressed. It is the equivalent to:

Input.GetKey(KeyCode.Space)

Since the horizontal axis is working but the vertical is not, I do not understand why the problem exists. Although I will continue trying to solve this, I would appreciate any help.

It turned out to be a problem with the conversion of ‘moveDirection’ from moving the player based on the universe’s XYZ axes to the player’s own XYZ axes.

That conversion took place at line 70, and led to the Z axis having to be represented as the Y axis in the new Vector3 creation at line 69.

moveDirection = new Vector3(horizontal, vertical, 0);

The coding confusion between the Y and Z axes caused them to swap themselves. Consequently, going forward caused the character to be pushed towards the ground, while the collider wouldn’t allow it to fall further. It thus looked like the forward key did nothing. The backward key caused the character to be pushed upwards, thus it performed a jump.

@Owen Reynolds You were right. I found this by using many notations and finding out that the Y axis was being affected by what should have been the Z axis. Consequently, swapping them worked perfectly. Jumping still worked since it wasn’t included in the Vector3 creation at line 69.