/// <summary> /// This method adds 1 to the player score based on the gameMode they are playing. The scores Are kept individually, /// because some of the gameModes are more difficult than others... so there is a Classic,Classic, and RelaxModeScore. /// </summar /// <summary> /// This Method is responsible for "destroying" our fruit if we swipe across them. The ray-cast comes from the /// camera and ends at the location of the fruit(after ScreenPointToRay(). /// </summary> private void CheckForRaycastHit() { //create a RaycastHit var and name it hit; RaycastHit hit; //create a Ray named ray, and give it the value of ScreenPointToRay(our fingerPos) Ray ray = mainCamera.ScreenPointToRay(fingerPos); //Ray-cast into our scene but only to our custom layer mask (myFruitAndPowerUpLayerMask)... //which only contains the "Fruit", and "PowerUp/Bomb" Layers. We are only ray-casting 100 units //into scene if (Physics.Raycast(ray, out hit, 100f, sliceableObjects)) { //we store our hit object in a new GameObject we create named hitObj. GameObject hitObj = hit.transform.gameObject; //Debug.Log("Ray-cast is doing its job"); //if the hitObj has a tag of "Fruit"... if (hitObj.CompareTag("Fruit")) { //make sure the obj is still active in the scene(make sure the onTriggerEnter event did not //already start deactivating this gameobject) if (hitObj.activeInHierarchy) { //gibs to use is assigned the value that SpawnProperFruitDebris() returns. We will need //this for later when we destroy the fruit. gibsToUse = SpawnProperFruitDebris(); //we Create a variable labeled "destroy" and we GetComponent() on the hitObj. DestroyFruit destroy = hitObj.GetComponent <DestroyFruit>(); //then we call the CutFruit() method on our hitObj, and pass the gibsToUse var as a parameter. destroy.CutFruit(/*debrisRot,*/ gibsToUse); } } else { //else since the tag is not "Fruit" and there was only 2 objects on our LayerMask(which is Fruit and PowerUp/Bombs) //then it has to be a power-up or a bomb... so we assume it is and move forward. if (hitObj.activeInHierarchy) { //we again create a new variable but this time its for our PowerUp/Bomb class "DestroyBombOrPowerUp". We // GetComponent on our objHit and then after words we call the "ActivateDestructionPerObjecttype()". DestroyBombOrPowerUp destroy = hitObj.GetComponent <DestroyBombOrPowerUp>(); //call the "ActivateDestructionPerObjecttype()" (this method will check to see what type of bomb or power up //it is and destroy itself/activate effects accordingly. destroy.ActivateDestructionPerObjecType(); } } } }
/// <summary> /// OnTriggerEnter This Unity Function checks if any "Fruit" are in our TriggerCollider. There is a long /// collider that protrudes into the scene, that is activated when the slicer is moving. Most of the time /// the ray-casting is responsible for the fruit "destroy" but occasionally the slice collider is responsible. /// </summary> /// <param name="other"></param> public void OnTriggerEnter(Collider other) { //if the object in our trigger collider is a "Fruit" then... if (other.CompareTag("Fruit")) { //if the other object is also active in the scene hierarchy(we make this check because we also have a ray-caster destroying fruit... we don't want them to get a hold //of the same fruit simultaneously... so to be safe we ask... Is it still active? because if not it might be because the ray-cast already started killing it.. if (other.gameObject.activeInHierarchy) { //commented for release. //Debug.Log("Collider is doing its job"); //we assign our "gibsToUse" variable the returned value of the Method SpawnProperFruitDebris()... (see that Method for better understanding). gibsToUse = SpawnProperFruitDebris(); //now we create a DestroyFruit variable named "destroy". We then grab the "other" gameObject that entered our trigger, and we cache its DestroyFruit Component in our new //"destroy" variable using GetComponent<>(); DestroyFruit destroy = other.GetComponent <DestroyFruit>(); //then we call "CutFruit()" on our destroy variable, and pass it our "gibsToUse" variable. destroy.CutFruit(gibsToUse); } } //if the object in our trigger collider is not a "Fruit", but it's a "BombOrPowerUp" then we have a similar approach but with a different class & method. if (other.CompareTag("BombOrPowerUp")) { //again we make sure that the object is in fact still active in the scene hierarchy... hopefully because we don't want to try to destroy it the same time as the ray-casting counterpart. if (other.gameObject.activeInHierarchy) { //commented out for release. //Debug.Log("Power-up/Bomb Being Destroyed Via Collider"); //now we create a DestroyBombOrPowerUp var named "destroy", and we use GetComponent<>() to store other.gameObjects DestroyBombOrPowerUp script... DestroyBombOrPowerUp destroy = other.GetComponent <DestroyBombOrPowerUp>(); //then we call ActivateDestructionPerObjectType() on our new destroy variable. destroy.ActivateDestructionPerObjecType(); } } }
/// <summary> /// OnTriggerEnter This Unity Function checks if any "Fruit" are in our TriggerCollider. There is a long /// collider that protrudes into the scene, that is activated when the slicer is moving. Most of the time /// the ray-casting is responsible for the fruit "destroy" but occasionally the slice collider is responsible. /// </summary> /// <param name="other"></param> public void OnTriggerEnter(Collider other) { //if the object in our trigger collider is a "Fruit" then... if (other.CompareTag("Fruit")) { //if the other object is also active in the scene hierarchy(we make this check because we also have a ray-caster destroying fruit... we don't want them to get a hold //of the same fruit simultaneously... so to be safe we ask... Is it still active? because if not it might be because the ray-cast already started killing it.. if (other.gameObject.activeInHierarchy) { string s = other.gameObject.name; //string ss = slicerString; slicerString += s.Substring(9, 1); //if(!CheckWords()) { // if(GameVariables.correct.Length > 0) { // GameController.GameControllerInstance.AddToFruitDestroyedScore(GameVariables.correct.Length, true); // GameVariables.correct = ""; // } // if(LauncherController.dictionary.Contains(ss.ToLower())) { // // GameController.GameControllerInstance.AddToFruitDestroyedScore(ss.Length, true); // slicerString = s.Substring(9, 1); // //} else { // // GameController.GameControllerInstance.AddToFruitDestroyedScore(ss.Length); // // slicerString = ""; // } else { // slicerString = ""; // } //} else { // if(LauncherController.dictionary.Contains(slicerString.ToLower())) { // GameVariables.correct = slicerString; // } //} //data = other.gameObject.GetComponent<WordData>(); //if (!listCollider.ContainsKey(data)) //{ // listCollider.Add(data, other.gameObject); //} //GameController.GameControllerInstance.AddToFruitDestroyedScore(); //commented for release. //Debug.Log("Collider is doing its job"); //now we create a DestroyFruit variable named "destroy". We then grab the "other" gameObject that entered our trigger, and we cache its DestroyFruit Component in our new //"destroy" variable using GetComponent<>(); DestroyFruit destroy = other.GetComponent <DestroyFruit>(); //then we call "CutFruit()" on our destroy variable, and pass it our "gibsToUse" variable. destroy.CutFruit(gibsToUse); //we assign our "gibsToUse" variable the returned value of the Method SpawnProperFruitDebris()... (see that Method for better understanding). } } //if the object in our trigger collider is not a "Fruit", but it's a "BombOrPowerUp" then we have a similar approach but with a different class & method. if (other.CompareTag("BombOrPowerUp")) { //again we make sure that the object is in fact still active in the scene hierarchy... hopefully because we don't want to try to destroy it the same time as the ray-casting counterpart. if (other.gameObject.activeInHierarchy) { //commented out for release. //Debug.Log("Power-up/Bomb Being Destroyed Via Collider"); //now we create a DestroyBombOrPowerUp var named "destroy", and we use GetComponent<>() to store other.gameObjects DestroyBombOrPowerUp script... DestroyBombOrPowerUp destroy = other.GetComponent <DestroyBombOrPowerUp>(); //then we call ActivateDestructionPerObjectType() on our new destroy variable. destroy.ActivateDestructionPerObjecType(); } } }