Problem moving up left and shooting at the same time in 2d game

I’m able to move in 8 directions, and shoot in all but the top left direction. Shooting and moving in the other 7 directions works, but when i move diagonally to the top and left i cant shoot, or if im moving left or up and im pressing space to shoot i cant change it to diagonal movement by additionally pressing up/left.

I tried putting the shoot function into a LateUpdate() function with the idea that that update would run after the rest but to no avail.

Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
public float speed = 8f;
public float padding = .5f;
public GameObject projectile;
public float projectileSpeed;

float xmin;
float xmax;
float ymin;
float ymax;

// Use this for initialization
void Start () {
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3 (0,0,distance));
//returns x and y between 0 and 1, (.5,.5) is the center (0,0) is bottom left. Z is distance from the camera
Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3 (1,0,distance));

Vector3 topmost = Camera.main.ViewportToWorldPoint (new Vector3 (0,1,distance));
//returns x and y between 0 and 1, (.5,.5) is the center (0,0) is bottom left. Z is distance from the camera

Vector3 bottommost = Camera.main.ViewportToWorldPoint (new Vector3 (0,0,distance));

xmin = leftmost.x + padding;
xmax = rightmost.x - padding;
ymin = bottommost.y + padding;
ymax = topmost.y - padding;
}

// Update is called once per frame
void Update () {

if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += new Vector3 (-speed * Time.deltaTime, 0, 0);
//transform.position += Vector3.left * speed * Time.deltaTime;

}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += new Vector3 (speed * Time.deltaTime, 0, 0);

}

if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += new Vector3 (0, speed * Time.deltaTime, 0);

}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position += new Vector3 (0, -speed * Time.deltaTime, 0);

}

//restricts the player to the gamespace, works in 3d
float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
transform.position = new Vector3 (newX, transform.position.y, transform.position.z);

float newY = Mathf.Clamp (transform.position.y, ymin, ymax);
transform.position = new Vector3 (transform.position.x, newY, transform.position.z);

}

void LateUpdate(){
if (Input.GetKeyDown(KeyCode.Space)){
GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
beam.GetComponent ().AddForce (new Vector2 (0f, 1000f));

}
}
}

it could be just input hardware limitation from the keyboard (Key rollover - Wikipedia).

try to use input controller instead (not sure, if it will help with the keyboard) and remap fire to an other key (not space. Space can make troubles if other keys are pressed)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 8f;
    public float padding = .5f;
    public GameObject projectile;
    public float projectileSpeed;

    float xmin;
    float xmax;
    float ymin;
    float ymax;


    // Use this for initialization
    void Start()
    {
        float distance = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
        //returns x and y between 0 and 1, (.5,.5) is the center (0,0) is bottom left. Z is distance from the camera
        Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));

        Vector3 topmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distance));
        //returns x and y between 0 and 1, (.5,.5) is the center (0,0) is bottom left. Z is distance from the camera

        Vector3 bottommost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));

        xmin = leftmost.x + padding;
        xmax = rightmost.x - padding;
        ymin = bottommost.y + padding;
        ymax = topmost.y - padding;
    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        transform.position += new Vector3(x * speed * Time.deltaTime, y * speed * Time.deltaTime, 0);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
            beam.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 1000f));
        }

        /*
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
            //transform.position += Vector3.left * speed * Time.deltaTime;

        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.position += new Vector3(speed * Time.deltaTime, 0, 0);

        }


        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.position += new Vector3(0, speed * Time.deltaTime, 0);

        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += new Vector3(0, -speed * Time.deltaTime, 0);

        }
        */

        //restricts the player to the gamespace, works in 3d
        float newX = Mathf.Clamp(transform.position.x, xmin, xmax);
        transform.position = new Vector3(newX, transform.position.y, transform.position.z);

        float newY = Mathf.Clamp(transform.position.y, ymin, ymax);
        transform.position = new Vector3(transform.position.x, newY, transform.position.z);


    }

    /*
    void LateUpdate()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
            beam.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 1000f));

        }
    }
    */
}
1 Like

Thank you, using another key worked. It turns out this keyboard cant detect input from Up and left arrow and space at the same time.