-1
\$\begingroup\$

I'm using the line of code if (Input.GetKey("left")), but it obviously didn't register the input of the arrow key. How can I detect arrow keys like this?

My code is as follows:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;
    public float forwardForce = 2750f;
    public float sidewaysForce = 150f;

    void FixedUpdate()
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("a"))
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        else
        {
            if (Input.GetKey("left")
            {
                rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
            }
        }

        if (Input.GetKey("d"))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        else
        {
            if (Input.GetKey("right"))
            {
                rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
            }
        }

        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Not what you asked about, but wouldn't it be more robust to use built-in functionalities (axes) rather than ad-hoc reading keyboard state? \$\endgroup\$
    – wondra
    Dec 14, 2019 at 16:59

1 Answer 1

0
\$\begingroup\$

The left is the correct name of the Left arrow key according to the documentation. But as advised in the comments, it's advised to use the axes of the Input manager so that the user will be able to customize the input keys, or use the overload of GetKey taking KeyCode as argument for a more robust solution if you want to "manually" check for the individual keys.

It's often advised to handle input in the Update function (but not necessary depending on your Physics configuration)

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    // This is a reference to the Rigidbody compenent called "rb"
    public Rigidbody rb;

    // This creates variables for the movement
    public float forwardForce = 2750f;
    public float sidewaysForce = 150f;
    private Vector3 force;

    void Update()
    {
        force.Set(0, 0, 0);

        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }
        else
        {    
            force.z = forwardForce;
            if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
                force.x = sidewaysForce;
            else if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
                force.x = -sidewaysForce;

            force *= Time.fixedDeltaTime;
        }
    }

    // We marked this as "fixed"update because we are using it to mess with Physics
    // In this case Unity likes FixedUpdate a lot better than Update
    void FixedUpdate()
    {
        rb.AddForce(0, 0, force.z);
        rb.AddForce(force.x, 0, 0, ForceMode.VelocityChange);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ GetKey does not use the Input Manager. You may be thinking of GetButton? \$\endgroup\$
    – DMGregory
    Dec 14, 2019 at 18:13
  • \$\begingroup\$ Oh you are right, I read the documentation too quickly. GetKey will report the status of the named key. This might be used to confirm a key is used for auto fire. For the list of key identifiers see Input Manager. \$\endgroup\$
    – Hellium
    Dec 14, 2019 at 19:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .