Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************
- * Authors:
- * 1) Lu Zhang <[email protected]>
- **************************************************************************/
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class WallTrapObject : uLink.MonoBehaviour
- {
- public float idleTimer;
- // bool isPlayerInside;
- public AudioSource wallTrapAudioSource;
- public AudioClip dropClip;
- Collider TrapCollider;
- // public WallTrapObject()
- // {
- //
- // }
- List<Collider> insidePlayers = new List<Collider>();
- void Awake()
- {
- TrapCollider = GetComponent<Collider>();
- TrapCollider.isTrigger = false;
- // isPlayerInside = false;
- GrabInsidePlayers();
- }
- void Start()
- {
- if (wallTrapAudioSource)
- {
- wallTrapAudioSource.PlayOneShot(dropClip);
- }
- StartCoroutine(SelfDestroy());
- }
- void GrabInsidePlayers() {
- Collider[] cols = Physics.OverlapSphere(transform.position, GetComponent<Renderer>().bounds.extents.magnitude, LayerMask.GetMask("Player"));
- foreach (Collider col in cols)
- {
- insidePlayers.Add(col);
- Physics.IgnoreCollision(col, TrapCollider, true);
- Debug.Log("Ignoring collision");
- }
- }
- // void OnTriggerEnter(Collider Other)
- // {
- // if (TrapCollider.isTrigger == true)
- // {
- // if (Other.tag == "player" || Other.tag == "Player")
- // {
- // if (Other.GetComponent<uLinkNetworkView>().owner == uLink.Network.player)
- // {
- // Debug.Log("Entering walltrap" + Other.ToString());
- // isPlayerInside = true;
- // }
- // else
- // {
- // Physics.IgnoreCollision(Other.GetComponent<Collider>(), TrapCollider, true);
- // insidePlayers.Add(Other.GetComponent<Collider>());
- // }
- // }
- // }
- // }
- // void OnTriggerExit(Collider Other)
- // {
- // if (Other.tag == "player" || Other.tag == "Player")
- // {
- // if (Other.GetComponent<uLinkNetworkView>().owner == uLink.Network.player)
- // {
- // Debug.Log("Exiting walltrap: " + Other.ToString());
- // isPlayerInside = false;
- // }
- // else
- // {
- // if(insidePlayers.Contains(Other.GetComponent<Collider>())) {
- // Physics.IgnoreCollision(Other.GetComponent<Collider>(), TrapCollider, false);
- // insidePlayers.Remove(Other.GetComponent<Collider>());
- // }
- // }
- // }
- // }
- void LateUpdate()
- {
- if(insidePlayers.Count > 0) {
- List<Collider> newInsidePlayers = new List<Collider>();
- Collider[] cols = Physics.OverlapSphere(transform.position, GetComponent<Renderer>().bounds.extents.magnitude, LayerMask.GetMask("Player"));
- foreach (Collider col in cols)
- {
- Debug.Log(col.gameObject.name + " in overlap");
- newInsidePlayers.Add(col);
- }
- for (int i = 0; i < insidePlayers.Count; i++)
- {
- if (!newInsidePlayers.Contains(insidePlayers[i]))
- {
- Debug.Log("Turning collision back on");
- Physics.IgnoreCollision(insidePlayers[i], TrapCollider, false);
- }
- }
- insidePlayers = newInsidePlayers;
- }
- // if (!isPlayerInside)
- // {
- // Debug.Log("TrapCollider.isTrigger = false;");
- // TrapCollider.isTrigger = false;
- // }
- }
- public IEnumerator SelfDestroy()
- {
- yield return new WaitForSeconds(idleTimer); // waits for several seconed
- GameObject.Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment