Search Unity

Objects Won't Ignore Each Other [SOLVED]

Discussion in 'Scripting' started by BrodiMAN, Jul 25, 2017.

  1. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    82
    Weird occurrence in my game.

    I have Power Ups in my game that randomly spawn. My player can shoot objects like lasers and such. I've used C# scripting to tell the projectiles to ignore the Power Ups. I've even placed them on different "layers". Yet, there are times when the two objects will still have an effect on each other.

    Example 1 - Power Up spawns while enemy laser is already moving. Enemy laser and Power Up collide causing the laser to lazily move in a random direction determined by physics. Yet, subsequent lasers correctly ignore the Power Up.

    Example 2- Even as the subsequent laser ignore the Power Up, they still slow down when flying through the power up. If I tested this by lining up 3 Power Ups in a row and had an enemy laser fly through them, the laser would "ignore" but clearly be effected by friction and slow down to an almost dead stop upon exiting the last Power Up.

    This is the script for the lasers that is instructing them to ignore certain objects in certain layers:

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         Physics.IgnoreLayerCollision(11, 9, true); //Ignore PowerUps
    4.         Physics.IgnoreLayerCollision(9, 9, true); //Ignore Prefabs of itself
    5.         Physics.IgnoreLayerCollision(8, 8, true); //enemy lases ignore Prefabs of itself
    6.         Physics.IgnoreLayerCollision(8, 9, true); //Ignore Enemy Lasers
    7.  
    8.         if (gameObject.tag == "EnemyWeapon")
    9.             Physics.IgnoreLayerCollision(8, 12, true); //Enemy Lasers Ignore Enemy Ships
    10.     }
    This is the code in the Power Ups that deals with ignoring collisions:

    Code (CSharp):
    1. Physics.IgnoreCollision(collision.collider, GetComponent<Collider>());
    Any advice?
     
  2. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    When you do this line in Power Ups? Maybe the GetComponent is too slow, and the line is not called when effects in game? Try to swap GetComponent on awake or public and assigned in editor, maybe change something
     
    BrodiMAN likes this.
  3. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    82
    This is the entirety of the script for my power ups in game:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PowerUpBehavior : MonoBehaviour {
    6.  
    7.     private float puRotation;
    8.  
    9.     void Start ()
    10.     {  
    11.         puRotation = 125.0f;
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         GetComponent<Rigidbody>().rotation = Quaternion.Euler(90.0f, puRotation*Time.time, 0.0f);
    17.     }
    18.  
    19.     void OnCollisionEnter(Collision collision)
    20.     {
    21.         if (collision.gameObject.tag == "Player")
    22.         {
    23.             if (gameObject.tag == "WeaponPowerUp")
    24.                 AudioController.WpnPowerUp();              
    25.             if (gameObject.tag == "ShieldPowerUp" || gameObject.tag == "LifePowerUp")
    26.                 AudioController.PowerUp();
    27.  
    28.             Destroy(gameObject);
    29.         }          
    30.         else
    31.             Physics.IgnoreCollision(collision.collider, GetComponent<Collider>());
    32.     }
    33. }
    However, I did try to move the IgnoreCollision above the rest of the statement like so:
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.gameObject.tag != "Player")
    4.             Physics.IgnoreCollision(collision.collider, GetComponent<Collider>());
    5.         else if (collision.gameObject.tag == "Player")
    6.         {
    7.             if (gameObject.tag == "WeaponPowerUp")
    8.                 AudioController.WpnPowerUp();              
    9.             if (gameObject.tag == "ShieldPowerUp" || gameObject.tag == "LifePowerUp")
    10.                 AudioController.PowerUp();
    11.  
    12.             Destroy(gameObject);
    13.         }      
    14.     }
    Still no success.

    However, your answer got me thinking. The OnCollisionEnter function requires the two objects to have already collided with each other for it to kick in. At that point physics has already happened. Is there a way to simply get the object to ignore each others' existence before the collision?
     
  4. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    Yes, you can change the layer collision to "Layer Collision Matrix" in Edit/Project Settings/PhysicsManager
     
    BrodiMAN likes this.
  5. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    82
    Mino92, I'm going to give you credit for helping solve this. As I was looking into the layer collision matrix I realized that at no point in my scripts did I tell the Enemy Lasers and the Power Ups (both on different layers) to ignore each other. That was my issue...-_-

    Thanks dude for the assistance!
     
  6. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    You are welcome ;D