zrrz111

Untitled

Jun 1st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. /**************************************************************************
  2.  * Authors:
  3.  * 1) Lu Zhang <[email protected]>
  4.  **************************************************************************/
  5.  
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9.  
  10. public class WallTrapObject : uLink.MonoBehaviour
  11. {
  12.   public float idleTimer;
  13. //  bool isPlayerInside;
  14.  
  15.   public AudioSource wallTrapAudioSource;
  16.   public AudioClip dropClip;
  17.   Collider TrapCollider;
  18.  
  19. //  public WallTrapObject()
  20. //  {
  21. //
  22. //  }
  23.  
  24.   List<Collider> insidePlayers = new List<Collider>();
  25.  
  26.   void Awake()
  27.   {
  28.     TrapCollider = GetComponent<Collider>();
  29.     TrapCollider.isTrigger = false;
  30. //    isPlayerInside = false;
  31.  
  32.     GrabInsidePlayers();
  33.   }
  34.  
  35.   void Start()
  36.   {
  37.  
  38.     if (wallTrapAudioSource)
  39.     {
  40.       wallTrapAudioSource.PlayOneShot(dropClip);
  41.     }
  42.     StartCoroutine(SelfDestroy());
  43.   }
  44.  
  45.   void GrabInsidePlayers() {
  46.         Collider[] cols = Physics.OverlapSphere(transform.position, GetComponent<Renderer>().bounds.extents.magnitude, LayerMask.GetMask("Player"));
  47.         foreach (Collider col in cols)
  48.         {
  49.             insidePlayers.Add(col);
  50.             Physics.IgnoreCollision(col, TrapCollider, true);
  51.             Debug.Log("Ignoring collision");
  52.         }
  53.   }
  54.  
  55.  
  56. //  void OnTriggerEnter(Collider Other)
  57. //  {
  58. //    if (TrapCollider.isTrigger == true)
  59. //    {
  60. //        if (Other.tag == "player" || Other.tag == "Player")
  61. //        {
  62. //            if (Other.GetComponent<uLinkNetworkView>().owner == uLink.Network.player)
  63. //            {
  64. //                Debug.Log("Entering walltrap" + Other.ToString());
  65. //                isPlayerInside = true;
  66. //            }
  67. //            else
  68. //            {
  69. //                Physics.IgnoreCollision(Other.GetComponent<Collider>(), TrapCollider, true);
  70. //                insidePlayers.Add(Other.GetComponent<Collider>());
  71. //            }
  72. //        }
  73. //    }
  74. //  }
  75.  
  76. //  void OnTriggerExit(Collider Other)
  77. //  {
  78. //    if (Other.tag == "player" || Other.tag == "Player")
  79. //    {
  80. //        if (Other.GetComponent<uLinkNetworkView>().owner == uLink.Network.player)
  81. //        {
  82. //            Debug.Log("Exiting walltrap: " + Other.ToString());
  83. //            isPlayerInside = false;
  84. //        }
  85. //        else
  86. //        {
  87. //            if(insidePlayers.Contains(Other.GetComponent<Collider>())) {
  88. //                Physics.IgnoreCollision(Other.GetComponent<Collider>(), TrapCollider, false);
  89. //                insidePlayers.Remove(Other.GetComponent<Collider>());
  90. //            }
  91. //        }
  92. //    }
  93. //  }
  94.  
  95.   void LateUpdate()
  96.   {
  97.     if(insidePlayers.Count > 0) {
  98.         List<Collider> newInsidePlayers = new List<Collider>();
  99.         Collider[] cols = Physics.OverlapSphere(transform.position, GetComponent<Renderer>().bounds.extents.magnitude, LayerMask.GetMask("Player"));
  100.         foreach (Collider col in cols)
  101.         {
  102.             Debug.Log(col.gameObject.name + " in overlap");
  103.             newInsidePlayers.Add(col);
  104.         }
  105.  
  106.         for (int i = 0; i < insidePlayers.Count; i++)
  107.         {
  108.             if (!newInsidePlayers.Contains(insidePlayers[i]))
  109.             {
  110.                 Debug.Log("Turning collision back on");
  111.                 Physics.IgnoreCollision(insidePlayers[i], TrapCollider, false);
  112.             }
  113.         }
  114.         insidePlayers = newInsidePlayers;
  115.     }
  116. //    if (!isPlayerInside)
  117. //    {
  118. //            Debug.Log("TrapCollider.isTrigger = false;");
  119. //      TrapCollider.isTrigger = false;
  120. //    }
  121.   }
  122.  
  123.   public IEnumerator SelfDestroy()
  124.   {
  125.     yield return new WaitForSeconds(idleTimer); // waits for several seconed
  126.  
  127.     GameObject.Destroy(gameObject);
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment