Repeat Fire Coroutine doesn't work correctly

Hi,

I’m a student of ’ Complete C# Unity Developer 2D: Learn to Code Making Games’ course. In the lecture 93 ‘Repeat Fire Coroutine’, my ‘FireContinously’ method doesn’t work correctly (The first challenge of this lecture). When I holding down the fire key it doesn’t repeat firing. Only fire when I click or push the fire button once. My Player.cs code is following…

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

public class Player : MonoBehaviour
{
    // Configuration Parameters
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float padding = 1f;
    [SerializeField] GameObject laserPrefab;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.1f;

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

    // Start is called before the first frame update
    void Start()
    {
        SetUpMoveBoudaries();
    }

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

   
    private void SetUpMoveBoudaries()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; // Get Left Camera x boundaries
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; // Get Right Camera x boundaries

        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; // Get bottom Camera x boundaries
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding; // Get top Camera x boundaries
    }

    private void Move()
    {
        // Get Input for move in Horizontal (left or right) * Time.deltaTime (Framerate independent)
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);

        // Get Input for move in Vertical (up or down) * Time.deltaTime (Framerate independent)
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);

        transform.position = new Vector2(newXPos, newYPos);
    }

    private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            StartCoroutine(FireContinously());
        }
    }

    IEnumerator FireContinously()
    {
        Debug.Log("Fire!");
        GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject; // Quaternion.identity) = no rotaion, Create laser as GameObject
        laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
        yield return new WaitForSeconds(projectileFiringPeriod);
    }

}

My Unity version is 2018.4.12f1
Please help me, Thank you.

Hi,

The problem is the following: You want to achieve that a new laser gets instantiated every 0.1 seconds. This means you want to have a loop.

However, there is no loop in your code. The FireContinuously coroutine instantiates one laser, waits for 0.1 seconds and is done.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Try using input.GetButton rather then GetButtonDown

 private void Fire()
    {
        if (Input.GetButton("Fire1")) // should auto fire now.
        {
            StartCoroutine(FireContinously());
        }
    }

Thank you, it is my mistake.
I can fixed it now by adding loop.

Thank you very much.

Thank you very much. I will try it.

Please do not try Input.GetButton. The method will return true in each frame while the “Fire1” button is pressed down. This means that each frame a new coroutine will start. That’s not what you want.

Implement a loop, e.g. a while loop.

Hi Nina,
Thank you for your suggestion. I can fixed it now by adding while(true) { } loop. Thank you again!

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

Privacy & Terms