Laser defender with Xtreme tuning & homing missiles

Here’s my attempt at Laser Defender!
I’ve decided to focus on player’s weapons first:

Some Xtreme tuning of player’s main weapon:

Homing missiles:

Coding homing missiles proved to be a lot more challenging and tiring than I expected, so I’ve decided to take some rest and move onto Glitch Garden for now. Maybe I’ll come back to Laser Defender later, after I practice a bit more in Unity and C#.

PS: sorry for the bad video quality, can you recommend a better video recorder for me, please?

4 Likes

Those homing missiles are pretty cool!

1 Like

Mind so blown right now! :heart_eyes::ok_hand:

This is amazing!
Would you kindly share how you did it? Especially the triple laser in the first video. That seems like something for my level.

I’m glad you’ve liked it!
What it’s basically doing is spawning projectiles at a certain degree, depending on variable shotCount.
The code is kinda heavy, but I’m sure mine isn’t the most efficient :sweat_smile:

IEnumerator FireFromPoint()
    {
        while (true)
        {
            yield return new WaitForSeconds(firingPeriod);
            PlayShotSFX();

            if ((shotCount % 2) == 0)
            {
                FireIfEven();
            }
            else
            {
                FireIfOdd();
            }
        }
    }

    private void FireIfEven()
    {
        for (int i = 0; i < shotCount / 2; i++)
        {
            CreateProjectileInstance(angleInRad * (i + 0.5f));  //creating projectiles on the right
            CreateProjectileInstance(-angleInRad * (i + 0.5f)); //creating projectiles on the left
        }
    }

    private void FireIfOdd()
    {
        CreateProjectileInstance();
        for (int i = 1; i <= shotCount / 2; i++)
        {
            CreateProjectileInstance(angleInRad * i);   //creating projectiles on the right
            CreateProjectileInstance(-angleInRad * i);  //creating projectiles on the left
        }
    }

    private void CreateProjectileInstance()
    {
        if (projectilePrefab)
        {
            GameObject projectile = Instantiate(
                projectilePrefab,          //prefab
                transform.position,        //position
                Quaternion.identity);      //rotation
            projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileVelocity);
        }
        else Debug.LogError("Missing object projectilePrefab, " + gameObject.name);
    }

    private void CreateProjectileInstance(float angleInRad)
    { 
        if (projectilePrefab)
        {
            //velocity vector
            Vector2 velocity = new Vector2(
                projectileVelocity * Mathf.Sin(angleInRad), 
                projectileVelocity * Mathf.Cos(angleInRad));
            //rotation in velocity vector direction
            Quaternion rotation = Quaternion.AngleAxis(-angleInRad * Mathf.Rad2Deg, new Vector3(0, 0, 1));

            GameObject projectile = Instantiate(
                projectilePrefab,   //prefab
                transform.position, //position
                rotation);          //rotation
            projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x, velocity.y);
        }
        else Debug.LogError("Missing object projectilePrefab, " + gameObject.name);
    }
1 Like

WoW Thank you! Yeah… it might be a bit advanced for my level seems like :DD
Thank you very much nonetheless!

Privacy & Terms