Yes, exactly.
Is this something that should be added in the movement script somehow, or can it be toggled on/off somewhere in the inspector?
Do you already know if-statements? If so, you could create one if and one else if to check the user input.
At the moment, we don’t check for “horizontal” or “vertical” user input. We use use the user input “as it is”. See here:
void MovePlayer()
{
float xValue = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float zValue = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(xValue,0,zValue);
}
If you don't know if-statements or would like to get a hint:
void MovePlayer()
{
float xValue = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float zValue = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
// "horizontal input"
if (xValue != 0f) { transform.Translate(xValue, 0f, 0f); }
// "vertical input"
else if (???) { transform.Translate(???); }
}
Since floats are often dangerous in regards to precision, it is better not to check for exactly 0. When you use a gamepad, you often don’t get exactly 0 even if you don’t move the stick.
For this reason, it is advisable check against a threshold. In this case, I would simply check the absolute value. Unity has got a method for that: Mathf.Abs(xValue)
. And then you could check: Mathf.Abs(xValue) > 0.01f
, where 0.01f is my threshold. Of course, you could use another value.