My fire button only works sporadically

I’m having a weird issue where my laser only Instantiates some of the time I press the fire button. I’m pretty sure I got the code right, but the button press is very unresponsive. Does anyone have an idea what I’m missing?

 //configuration parameters
    [SerializeField] float speed = 5f;
    [SerializeField] float clampPadding = 0.4f;
    [SerializeField] float laserSpeed = 5f;


    //references
    private Rigidbody2D myRigidbody;
    [SerializeField] private GameObject myLaserPrefab1;

    float xMin;
    float xMax;
    float yMin;
    float yMax;


    // Start is called before the first frame update
    void Start()
    {
        //myRigidbody = GetComponent<Rigidbody2D>();
        SetUpMoveBoundaries();
    }

    private void SetUpMoveBoundaries()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0,0,0)).x;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y;

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Move();

        Fire();
    }

    private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            GameObject laser = Instantiate(myLaserPrefab1, transform.position, Quaternion.identity) as GameObject;
            laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, laserSpeed);
        }

    }

    private void Move()
    {
        var deltaX = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
        var deltaY = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;


        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin + clampPadding, xMax - clampPadding);

        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin + clampPadding, yMax - clampPadding);

        Vector2 movement = new Vector2(newXPos, newYPos);
        //movement.Normalize();


        transform.position = movement;

        //myRigidbody.MovePosition(myRigidbody.position + movement);
    }

Post your code

Hi Niall,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the lasers do get instantiated but get rendered behind the background.

Is the z-position of the background set to 10, the z-position of the camera to -10 and the z-position of the other game objects to 0?

I posted the code as requested.

I have tried debug.log, it seems to have made the issue worse some how. The debug only shows in the console when the laser does, which is now even less often. The background was in the correct position. I have posted my code.

It is not recommended to check the user input in the FixedUpdate method. Maybe that’s the reason why the lasers do not appear when expected. Furthermore, one must not use Time.deltaTime in the FixedUpdate method.

Replace FixedUpdate with Update to see if that fixes the issue.

That solved the issue. I was originally playing around with using rigidbody for movement and I forgot to change it back. Thank you for your help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms