Search Unity

having a hard time with multiple colliders

Discussion in 'Scripting' started by tawak3500, Jul 29, 2016.

  1. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    Hi. I'm trying to create a multiplayer platformer and I'm trying to create a store. When the player is over the store, a text that saids "press A" appears. Once the player gets the item text will disappear even when the player is over the shop collider.

    This is fine for 1 player but I'm having a really hard time doing this with multiple players. I want the text to show when theres 1 or more players colliding with the store and within those players if anyone doesn't have an item the text will be displayed.

    Is there a way to find out exactly what objects are overlapped with the collider?

    Heres my code but I feel like i'm over complicating things. And it only works when theres 1 player.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class InventoryShopList : MonoBehaviour
    5. {
    6.     public string crossbow,bomb;
    7.     public enum InventoryItem { crossbow,bomb}
    8.     public InventoryItem myInventoryItem;
    9.     public string pickupID;
    10.  
    11.     private PlayerScript _playerscript;
    12.     private SpriteRenderer _buttonDisplay;
    13.     private bool _isTouching, _hasInventory;
    14.     private PlayerScript _p1,_p2, _p3, _p4;
    15.     private bool _bool1,_bool2, _bool3, _bool4, _anyPlayerWithInventory;
    16.  
    17.  
    18.     //sets up the inventory for the player
    19.     public GameObject GetInventory (GameObject go)
    20.     {
    21.         switch (myInventoryItem)
    22.         {
    23.             case InventoryItem.crossbow:
    24.                 go = Resources.Load<GameObject>("Prefabs/StartPointCrossbow");
    25.                 pickupID = crossbow;
    26.                 break;
    27.  
    28.             case InventoryItem.bomb:
    29.                 go = Resources.Load<GameObject>("Prefabs/StartPointCrossbow");
    30.                 pickupID = bomb;
    31.                 break;
    32.  
    33.             default:
    34.                  break;
    35.         }
    36.         return go;
    37.     }
    38.  
    39.  
    40.     void Start()
    41.     {
    42.         _buttonDisplay = transform.Find ("button_a").GetComponent<SpriteRenderer>();
    43.     }
    44.  
    45.  
    46.     void OnTriggerEnter2D (Collider2D col)
    47.     {
    48.         if (col.tag == "Player")
    49.         {
    50.             //_isTouching = true;
    51.             _playerscript = col.GetComponent<PlayerScript>();
    52.             if (_playerscript.playerID == PlayerScript.PlayerID.Player1)    _p1 = col.GetComponent<PlayerScript>();
    53.             if (_playerscript.playerID == PlayerScript.PlayerID.Player2)    _p2 = col.GetComponent<PlayerScript>();
    54.             if (_playerscript.playerID == PlayerScript.PlayerID.Player3)    _p3 = col.GetComponent<PlayerScript>();
    55.             if (_playerscript.playerID == PlayerScript.PlayerID.Player4)    _p4 = col.GetComponent<PlayerScript>();
    56.         }  
    57.     }
    58.  
    59.     public bool IsTouching() //check if player/s is touching the collider
    60.     {
    61.         _isTouching = gameObject.GetComponent<BoxCollider2D>().IsTouchingLayers(LayerMask.GetMask("Player"));
    62.         return _isTouching;
    63.     }
    64.      
    65.     void Update()
    66.     {
    67.           IsTouching();
    68.         if (_isTouching)
    69.         {
    70.             if (_p1 != null) _bool1 = _p1.hasInventory; else _bool1 = false;
    71.             if (_p2 != null) _bool2 = _p2.hasInventory; else _bool2 = false;
    72.             if (_p3 != null) _bool3 = _p3.hasInventory; else _bool3 = false;
    73.             if (_p4 != null) _bool4 = _p4.hasInventory; else _bool4 = false;
    74.         }
    75.         if (!_isTouching)
    76.         {
    77.             if (_bool1 == true) _bool1 = false;
    78.             if (_bool2 == true) _bool2 = false;
    79.             if (_bool3 == true) _bool3 = false;
    80.             if (_bool4 == true) _bool4 = false;
    81.         }
    82.            
    83.         _anyPlayerWithInventory = _bool1 | _bool2 | _bool3 | _bool4;
    84.  
    85.         if(_playerscript != null)
    86.         {
    87.             if( _isTouching)
    88.             {
    89.                     _buttonDisplay.enabled = true;
    90.             }
    91.  
    92.             if (!_isTouching || _anyPlayerWithInventory)
    93.             {
    94.                 _buttonDisplay.enabled = false;
    95.  
    96.             }
    97.         }
    98.     }
    99. }
    100.