Sample Code Basswings
The City Block and its Building "Toggles Collision Boxes/Mesh"
|
|
Frame Class [city block]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Frame : MonoBehaviour
{
public PickUp[] m_Pickups;
public BoxCollider[] m_ColBoxes;
Bounds m_FrameBounds;
GameObject[] m_players = null;
bool m_bIsPlayerInside = false;
// Use this for initialization
void Start ()
{
m_Pickups = GetComponentsInChildren();
m_ColBoxes = GetComponentsInChildren();
//m_players = GamePlay.Get().GetPlayers();
m_players = GameObject.FindGameObjectsWithTag("Player");
m_FrameBounds = m_ColBoxes[0].bounds;
for(int i = 1; i < m_ColBoxes.Length; i ++)
{
m_ColBoxes[i].gameObject.AddComponent();
}
ActivateCollsionBoxes(false);
}
public void LateUpdate()
{
m_players = GamePlay.Get().GetPlayers();
}
public void Update()
{
if(!m_bIsPlayerInside)
{
m_bIsPlayerInside = IsPlayerInside();
if(m_bIsPlayerInside)
{
ActivateCollsionBoxes(true);
}
}
//Turn Off
if(m_bIsPlayerInside)
{
m_bIsPlayerInside = IsPlayerInside();
if(!m_bIsPlayerInside)
{
ActivateCollsionBoxes(false);
}
}
}
bool IsPlayerInside()
{
for(int i = 0; i< m_players.Length; i ++)
{
if(m_FrameBounds.Contains(m_players[i].transform.position))
{
return true;
}
}
return false;
}
public void Reset()
{
m_FrameBounds = GetComponentInChildren().bounds;
m_bIsPlayerInside = false;
foreach(PickUp p in m_Pickups)
p.Activate();
}
///
/// Deactivate collsion boxes except at index 0.
///
///
/// Activate collsion boxes except at index 0.
///
void ActivateCollsionBoxes(bool toggle)
{
for(int i = 1; i < m_ColBoxes.Length; i ++)
{
m_ColBoxes[i].enabled = toggle;
}
}
void OnDrawGizmos()
{
Gizmos.DrawWireCube(m_FrameBounds.center, m_FrameBounds.size);
}
}
Building Class
using UnityEngine;
using System.Collections;
public class Building : MonoBehaviour
{
public MeshCollider[] m_MeshColl;
public void Start()
{
m_MeshColl = GetComponentsInChildren();
foreach(MeshCollider m in m_MeshColl)
m.gameObject.tag = "Building";
ToggleMeshCollider(false);
}
public void OnTriggerEnter(Collider collision)
{
if(collision.gameObject.tag == "Player")
{
print(collision.name);
ToggleMeshCollider(true);
}
}
public void OnTriggerExit(Collider collision)
{
if(collision.gameObject.tag == "Player")
{
ToggleMeshCollider(false);
}
}
void ToggleMeshCollider(bool toggle)
{
foreach(MeshCollider m in m_MeshColl)
m.enabled = toggle;
}
}