using UnityEngine; using System.Collections; public class CoinBox : MonoBehaviour { #region PUBLIC VARIABLES public GameObject poppedStatePrefab; // The prefab representing the struck (popped) version of the Coin Box #endregion #region COLLIDER 2D COMPONENT METHODS [OnEnter] /// /// Pops a coin out if the box is being struck from within the correct range of angles /// /// The Collider2D that is entering this trigger void OnTriggerEnter2D(Collider2D collider) { Vector3 heading = this.transform.position - collider.gameObject.transform.position; // Get the difference in position between this and the colliding object float distance = heading.magnitude; // Get the distance between the two objects based on the heading Vector3 direction = heading / distance; // Get the direction of contact between the two objects // If the direction is within the correct range, and the colliding GameObject is the player, pop the coin out if((direction.x < 0.1 && direction.x > -1.1) && (direction.y < 1.1 && direction.y > 0.4) && collider.tag == "Player") { CoinPop(); } Debug.Log(direction.ToString()); } #endregion #region OTHER METHODS [CoinPop] /// /// Deactivates this GameObject and activates the poppedStatePrefab GameObject instead /// void CoinPop() { poppedStatePrefab.SetActive(true); this.gameObject.SetActive(false); } #endregion }