Search Unity

Ignoring collision on all objects of type

Discussion in 'Scripting' started by Sniffi, Mar 4, 2015.

  1. Sniffi

    Sniffi

    Joined:
    Feb 9, 2015
    Posts:
    3
    Hello, i am making a platform jumper and it seems like i have run into a problem.

    I want my script to trigger in a more general manner, not specific only on objects set in the inspector.

    Example:
    My code is as follows:
    Code (csharp):
    1.  
    2. public class PlayerCollision : MonoBehaviour {
    3.  
    4.     public BoxCollider2D visiblePlatform;
    5.  
    6.     private bool triggered;
    7.    
    8.     // Update is called once per frame
    9.     void Update () {
    10.  
    11.         if(triggered == true){
    12.             visiblePlatform.enabled = false;
    13.         }
    14.         if (triggered == false) {
    15.             visiblePlatform.enabled = true;
    16.         }
    17.    
    18.     }
    19.  
    20.     void OnTriggerEnter2D(Collider2D other) {
    21.         triggered = true;
    22.     }
    23.  
    24.     void OnTriggerExit2D(Collider2D other) {
    25.         triggered = false;
    26.     }
    27. }
    28.  
    And while on the subject i have another question if its not too much to ask: my bounce script triggers on collision with the trigger and i don't want it to do so:

    The script:
    Code (csharp):
    1.  
    2. public class PlayerBounce : MonoBehaviour {
    3.  
    4.     void OnCollisionEnter2D(){
    5.             rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, 0);
    6.             rigidbody2D.AddForce (new Vector2 (0, 400));
    7.     }
    8. }
    9.  
    Thanks.
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    what do you mean "set in the inspector" ?
    what are you setting?
     
  3. Sniffi

    Sniffi

    Joined:
    Feb 9, 2015
    Posts:
    3
    I set the platform that has its collision turned off in the inspector and it works only on THAT platform, not on all, even though i try setting it to the prefab and not the game object. I tried getting the trigger and then disable the collision on its parent element but it didn't work either.
     
  4. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  5. Sniffi

    Sniffi

    Joined:
    Feb 9, 2015
    Posts:
    3
    Ok, so i used layers, but now the trigger doesn't work so the character falls through the platforms.

    The code is as follows:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerCollision : MonoBehaviour {
    5.  
    6.     //public BoxCollider2D visiblePlatform;
    7.     //public BoxCollider2D trigger;
    8.  
    9.     private bool triggered = false;
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         if(triggered == true){
    15.             //visiblePlatform.enabled = false;
    16.             Physics2D.IgnoreLayerCollision(9, 8, false);
    17.             Debug.Log (Physics2D.GetIgnoreLayerCollision(9, 8));
    18.         }
    19.         if (triggered == false) {
    20.             //visiblePlatform.enabled = true;
    21.             Physics2D.IgnoreLayerCollision(8, 9, true);
    22.         }
    23.    
    24.     }
    25.  
    26.     void OnTriggerEnter2D(Collider2D other) {
    27.         triggered = true;
    28.     }
    29.  
    30.     void OnTriggerExit2D(Collider2D other) {
    31.         triggered = false;
    32.     }
    33. }