Hi, I’m trying to make it so that when the player is within a certain of a non player character, and the player is pressing the LeftArrowKey, the RightArrowKey or A or D (in the WASD), an animation plays on the NPC.
I understood that “Horizontal” covered these buttons like Fire1 covers the LeftMouseButton.
If anyone would please point me to a solution that would be great.
// Update is called once per frame
void Update () {
bool down = Input.GetButtonDown("Horizontal");
bool held = Input.GetButton("Horizontal");
bool up = Input.GetButtonUp("Horizonal");
if (Vector3.Distance(Player.transform.position, NPC.transform.position) < Distance && Input.GetButtonDown("Horizontal")) {
NPC.GetComponent<Animation>().Play("Animation_One");
}
}
“Horizontal” is the name of an axis, not the buttons; it works with GetAxis.
if (Vector3.Distance(etc) < Distance && Input.GetAxis("Horizontal") != 0f)
Be advised that this is one of very rare instances where it’s okay to check for exact equality (== and != ) with floats, because 0 is the exact value that’s returned when the axis isn’t being pressed. Analog input or joysticks might make that dicey, though, if you allow non-keyboard input, so you’d want to check for a range instead if so.