/// <summary> /// Return the relative component to its pool in an deactivated state to be stored for later use, and level ending checks are done here. /// </summary> /// <param name="other">Generally other component is friendly or obstacle cube.</param> private void OnTriggerEnter(Collider other) { CollectableType.TYPE type = other.GetComponent <CollectableType>().type; if (GameManager.instance.hole.canMove || type.Equals(CollectableType.TYPE.FRIENDLY_FREE)) { ObjectPool.Reset(other.gameObject); // Return the object clone to its pool and reset transform attributes. if (type.Equals(CollectableType.TYPE.FRIENDLY)) { GameManager.instance.currentCollection++; // Increment current collection count. GameManager.instance.SetLevelIndicatorText((int)Calc.map(GameManager.instance.currentCollection, 0.0f, GameManager.instance.targetCollection, 0.0f, 100.0f)); if (GameManager.instance.currentCollection == GameManager.instance.targetCollection) { if (GameManager.instance.currentLevel % 2 == 0) { GameManager.instance.hole.Center(); } else { GameManager.instance.levelIndicatorText.SetText("You Win!"); GameManager.instance.levelLoader.Load(++GameManager.instance.currentLevel); } } } else if (type.Equals(CollectableType.TYPE.OBSTACLE)) { GameManager.instance.GameOver(); } } }
/// <summary> /// /// </summary> /// <param name="other">Other game object is generally the Cube object which has a kinematic rigidbody</param> private void OnTriggerEnter(Collider other) { CollectableType.TYPE type = other.GetComponent <CollectableType>().type; if (GameManager.instance.hole.canMove || type.Equals(CollectableType.TYPE.FRIENDLY_FREE)) { Rigidbody rigidBody = other.GetComponent <Rigidbody>(); // Get the rigidbody component. // If the game object does not have any rigidbody attached return directly. if (rigidBody == null) { // If it is null, print an error message Debug.LogError("RigidTrigger : " + " 'other' game object has no rigidbody. < probably you should check collision layers >"); return; } rigidBody.isKinematic = false; // Set the body to dynamic, so it can fall. float altitudeForce = transform.position.y * altitudeMagnitude; // Force is applied greater with altitude so it can fall to the hole better. Vector3 fallDirection = (transform.position - rigidBody.transform.position).normalized; // This is the normalized direction vector from the Cube to the Hole. rigidBody.AddForce(fallDirection * magnitude * altitudeForce); } }