Layers not working in Laser Defender Project

Changing layers for collision matrix doesn’t work in Laser Defender Project… Don’t know why, can anybody Help?

Hi Pulkit,

What do you mean by “doesn’t work”? What exactly does not work? What did you expect to happen, what happened instead?

Sorry for not explaining , Actually after disabling the collision as you can see in screenshot , objects are still colliding ( OnTrigger function calling ) with non colliding layers. For. example : Enemy is colliding with Enemy Projectile.

Did you double check whether the concerning game objects are on the correct layers?

Yes , I already checked them . Here is the complete issue , and sorry for replying late .

Changing layers for collision matrix doesn’t work in Laser Defender Project… Don’t know why can anybody Help?

Hi there, might need a little more information on what you’re trying to do and where its not working. Obviously, make sure that you have the right colliders and trigger settings etc on your game objects.

Thanks for replying :slightly_smiling_face: , This is the video for the issue I am facing right now.
Whats happening is Playerlaser projectile is still colliding with Player and same for Enemy and Enemy Projectile . You can see in the video that Player get destroyed when I fire three times (firing not visible in the video ) , and also enemy got destroyed when they collide with themself or with their own laser.
Please Help, Is there anything I did wrong?

My code for Player :

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

public class Player : MonoBehaviour
{
   // cache reference

        [Header("Player")]
    [Range(0, 20)] [SerializeField] float PlayerSpeed = 10f;
    [SerializeField] float padding = 0.5f;
    [SerializeField] int health = 200;

    [Header("Projectile")]
    
    [SerializeField] float laserspeed = 10f;
    [SerializeField] float firingRate = 0.1f;
    [SerializeField] GameObject laserPrefab;
    Coroutine fireContinously;


    float minX, maxX, minY, maxY;
    bool toFire = false;





    void Start()
    {
        
        SetupMoveBoundaries();
    }

    private void SetupMoveBoundaries()
    {
        Camera maincamera = Camera.main;
        minX = maincamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        maxX = maincamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        minY = maincamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        maxY = maincamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
       
    }

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

    private void Move()
    {
        float deltaX = Input.GetAxis("Horizontal")*Time.deltaTime*PlayerSpeed;
        float deltaY = Input.GetAxis("Vertical")*Time.deltaTime*PlayerSpeed;

        
        var newXPos = Mathf.Clamp( transform.position.x + deltaX,minX,maxX);
        var newYPos = Mathf.Clamp(transform.position.y + deltaY,minY,maxY);

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

    }

    private void Fire()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            toFire = true;
            fireContinously=StartCoroutine(FireLaser());


        }
        if(Input.GetKeyUp(KeyCode.Space))
        {
            toFire = false;
            StopCoroutine(fireContinously);
            

        }
       
    }

    IEnumerator FireLaser()
    {
        while (toFire)
        {

            GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
            laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, laserspeed);

            yield return new WaitForSeconds(firingRate);
        }
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("damageDealer.getDamage() called");
    
            DamageDealer damageDealer = collision.GetComponent<DamageDealer>();

            
        
                damageDealer.Hit();
          

            health -= damageDealer.getDamage();
            if (health <= 0)
            {
                Destroy(gameObject);
            }
            
      
    }


}

Enemy :

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

public class Enemy : MonoBehaviour
{
    [SerializeField] int health = 100;
    [SerializeField] float shotCounter;
    [SerializeField] float minTimeToShoot = 2f;
    [SerializeField] float maxTimeToShoot = 7f;
    [SerializeField] GameObject laserPrefab;
    [SerializeField] float laserspeed =5f;
   

    // Start is called before the first frame update
    void Start()
    {
        shotCounter = UnityEngine.Random.Range(minTimeToShoot, maxTimeToShoot);
        
    }

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

        shotCounter -= Time.deltaTime;
        if(shotCounter<=0)
        {
            Shoot();


            shotCounter = UnityEngine.Random.Range(minTimeToShoot, maxTimeToShoot);
        }
        
    }

    private void Shoot()
    {
        GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0,-laserspeed);
        
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
     
            DamageDealer damageDealer = collision.GetComponent<DamageDealer>();
            ManageHit(collision, damageDealer);
     
    }



    private void ManageHit(Collider2D collision, DamageDealer damageDealer)
    {
        health -= damageDealer.getDamage();
        if (health <= 0)
        {
            Destroy(gameObject);
        }
        damageDealer.Hit();
    }
}

Please refrain from posting in multiple threads because it is impossible for us to gather information on your problem if they are in multiple places. Furthermore, it might be that you will get ignored if it looks as though somebody else is already helping you in another thread.

I moved your posts to this thread, so it is easier for us to keep track of the problem.


Add the following to your OnTriggerEnter2D method to get more information about the colliding object. It might well be that there is something else destroying your ship.

private void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log("Object collided with Player: " + collision.gameObject.name);

    // your code
}

Play your game. When the player gets destroyed, check the console.

1 Like

With my limited knowledge and what’s been provided to me I have two things in mind, first double check that the enemySpawner is spawing the correct thing. What I mean by that is run your game wait for it to spawn a couple of enemies then check on them in the hierarchy.

The only suggestion I have currently is to try setting enemySpawner to the enemy layer as well. If the first thing I mention shows no results.

I know it is little stupid but if you check the screenshot again I accidentaly selected Physics in place of Physics2D , that’s the main issue , Sorry for that , I should double check that before posting :sweat_smile:.

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

Privacy & Terms