How to stop shooting myself? Laser Defender question

Hi everyone! I have a question on the Laser Defender course, instantiating lasers.

The problem that I’m running into is that my lasers are instantiating directly on top of my player ship, hitting it, and essentially “blowing myself up”.

The weird thing is, this only started after I switched my project from Mac to Windows. Any ideas why this could be, and how to fix it?

Here is the screenshot:

Here is the code:

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


public class weaponConfig : MonoBehaviour {


    [Header("Projectile")]
    [SerializeField] GameObject laserPrefab;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float projectileFiringPeriod = 0.1f;
    [SerializeField] AudioClip shootSound;
    [SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;
    [SerializeField] public static int numberOfLasers = 1;



    Coroutine firingCoroutine;
    int bBodyCounter;









            public float GetProjectileSpeed()
    {
        return projectileSpeed;
    }

    public float GetProjectileFiringPeriod()
    {
        return projectileFiringPeriod;
    }



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


    public void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            firingCoroutine = StartCoroutine(FireContinuously());
        }
        if (Input.GetButtonUp("Fire1"))
        {
            StopCoroutine(firingCoroutine);
        }
    }




    private IEnumerator FireContinuously()
    {
            while (true)
            {
                if (numberOfLasers == 1)
                {
                    FireOne();
                }
                if (numberOfLasers == 2)
                {
                    FireTwo();
                }
                if (numberOfLasers == 3)
                {
                    FireThree();
                }
                if (numberOfLasers == 4)
                {
                    FireFour();
                }
                if (numberOfLasers == 5)
                {
                    FireFive();
                }





            yield return new WaitForSeconds(projectileFiringPeriod);
        }
    }

    private void FireOne()
    {
        FireMiddle();
    }

    private void FireTwo()
    {
        FireMiddle();
        DoubleFire();
    }

    private void FireThree()
    {
        FireLeft();
        FireRight();
        FireMiddle();
    }

    private void FireFour()
    {
        FireLeft();
        FireRight();
        DoubleFire();
        DoubleFireNegative();

    }

    private void FireFive()
    {
        FireLeft();
        DoubleFire();
        DoubleFireNegative();
        FireRight();
        FireMiddle();
    }

    private void FireLeft()
    {
        GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(-2, projectileSpeed);
    }
    private void FireRight()
    {
        GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(2, projectileSpeed);
    }



    private void FireMiddle()
    {
        GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
       
    }


    private void DoubleFire()
    {
        GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(1, projectileSpeed);
    }

    private void DoubleFireNegative()
    {
        GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(-1, projectileSpeed);
    }







}

(I know the firing power-up structure is not the best lol)

Okay, so I figured it out and got it working. I’ll keep the thread and post the solution below I guess if anyone else runs into this as well:

It was layers. I didn’t have any layers set up on my Windows computer so when I transferred all of my files from Mac (which I’m sure there’s a better way to do it, I just used a flash drive), my Unity game engine settings / Project Settings didn’t transfer over, including layers.

Then I assigned each of my prefabs to that specific layer. Player to Player, Laser to Laser, etc.

So all I had to do was create the layers again, assign them then, open Edit > Project Settings > Physics 2D > Scroll down to the collision matrix:

And viola! I stop shooting myself.

1 Like

Good job! :slight_smile:

2 Likes

Thanks! :slight_smile:

1 Like

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

Privacy & Terms