Ok so I have a problem…
I’m making a top-down shooter and I created a player controller but…
The player ONLY moves if I press keys belonging to BOTH horizontal and vertical inputs.
(EG - It wont move if I press "W’ but only moves if I press “W” and “A” or “W” and “D”)
Even then, it only moves forward and backwards…
My Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject thePlayer;
public bool isRunning;
public float horizontalMove;
public float verticalMove;
public int moveSpeedHorizontal;
public int moveSpeedVertical;
void Update()
{
if (Input.GetButton("Horizontal"))
{
thePlayer.GetComponent<Animation>().Play("Walk");
verticalMove = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeedVertical;
isRunning = true;
transform.Translate(0, 0, verticalMove);
}
else if(Input.GetButton("Vertical"))
{
thePlayer.GetComponent<Animation>().Play("Strafe");
horizontalMove = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeedHorizontal;
transform.Translate(horizontalMove, 0, 0);
}
}
}
P.S - Yes I am using legacy animation if you were wondering
ALSO Using Unity 2018.3.0f.2 Because I’m scared my PC won’t support Unity 2019…
Why is it your GetButton(“Horizontal”) if check doesn’t do anything within it for Horizontal movement? It handles verticalMove inside it. And same with your GetButton(“Vertical”) handles Horizontal stuff in it.
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH I AM SO STUPID THANKS BRO
Additionally, you’ve made the axes mutually exclusive. If you want to be able to move diagonally, you want to combine the movement of both vertical and horizontal into one final movement value.
You said when you pressed both A and W it just moved you forward and backward, not diagonally. Which makes sense due to the issue laid out by @Brathnann . After having fixed that issue, if you made no other modifications to the code you posted, you would not be able to move diagonally.
Given that I am not omniscient, I don’t have access to the current state of your code at any given moment. If you were able to move diagonally after fixing it, then you made more changes than are implied by simply swapping Horizontal and Vertical, as I specified in my previous post. Since it appears that you did, you can disregard my post as you’ve already fixed it.