- Home /
The question is answered, right answer was accepted
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.
Throw in lots of debugs. In each if, print("TWO is down");
.
If they are held down for a while, make them public in the input manager, so you can see them. Or if it isn't serialized, just copy them: public bool[] I$$anonymous$$_shower=new bool[4];
and each update loop: I$$anonymous$$_shower[i]=input$$anonymous$$anager[i];
.
Answer by JusticeAShearing · Mar 15, 2015 at 05:18 PM
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.
Wait -- so your character didn't have Y facing up? That's the only way I can think TransformDirection would cause a problem. It pretty much just says "respect the blue arrow," which is usually what you want.
I still am unsure about why the problem occurred, but it has been solved, thus I have moved on. Perhaps I will further my efforts to discover the cause of the problem once I have released my game.
The blue arrow faces upwards and affects Y, the green arrow Z and the red arrow X.
"The blue arrow faces upwards". There's the problem. A lot of things in Unity think blue=Z=forwards. Your movement script thinks the player is lieing down.
Import from Blender? In Blender, Apply the rotation fix or try the new experimental fix button in FBX export.
@Owen Reynolds Yes, I see that you're correct after looking at several different objects from that same scene. Their blue arrows point forwards, not upwards. While creating my player, the mesh had a different rotation to the Character object (a parent of it) which the script was applied to, while I assumed that they both had the same rotation.
@meat5000 Thanks for the advice, because although this model wasn't a blender import, I will be importing my own blender model soon for purposes of character customization. Thus the advice may be useful in the near future.