- Home /
Question by
JMasterBoi · Jul 06, 2020 at 06:36 PM ·
movementbuttons
Why am I not Able to Press Multiple Buttons?
I made a controller script that moves the player (w = forward, s = backwards, etc.). However, I can only press one button at a time (so pressing forward + side DOESN'T move you diagonally, only forward or sideways depending on which button was pressed first). Is there a reason why? Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
// speeds
public float rotateSpeed;
public float moveSpeed;
public float jumpSpeed;
// player references
public GameObject Player;
public Transform Position;
public Rigidbody Physics;
public float Rotation;
public bool touchingGround = true;
private void Start()
{
Rotation = Position.rotation.y;
}
// Update is called once per frame
void Update()
{
// move forward
if (Input.GetKey("w"))
{
Position.Translate(0f, 0f, moveSpeed);
}
// move right
else if (Input.GetKey("d"))
{
Position.Rotate(0f, rotateSpeed, 0f);
}
// move left
else if (Input.GetKey("a"))
{
Position.Rotate(0f, -rotateSpeed, 0f);
}
// move back
else if (Input.GetKey("s"))
{
Position.Translate(0f, 0f, -moveSpeed);
}
// jump
else if (Input.GetButtonDown("Jump") && touchingGround == true)
{
Physics.velocity = Vector3.up * jumpSpeed;
}
}
private void OnCollisionEnter(Collision other)
{
string tag = other.collider.gameObject.tag;
if (tag == "Ground")
{
touchingGround = true;
}
}
private void OnCollisionExit(Collision other)
{
string tag = other.collider.gameObject.tag;
if (tag == "Ground")
{
touchingGround = false;
}
}
}
thank you in advance!
Comment
Best Answer
Answer by Razor1994 · Jul 06, 2020 at 08:09 PM
The most obvious thing that you need to change is your else if
statements.
You need to change them to just if
without the else. That is because when you press w and d, your first condition is met and therefor no else statement will be reached and your d key will not be recognized.