#10 Introducing the Cube Obliteration Zone

#10 Introducing the Cube Obliteration Zone

ยท

3 min read

Introduction:

Welcome, game developers, to the tenth chapter of our Beat Saber VR series! In this edition, we're setting the stage for a grand finale by creating a plane that will serve as the Cube Obliteration Zone. Once cubes gracefully touch this plane, they'll vanish into thin air, leaving a trail of rhythm in their wake. Let's embark on this last leg of our development journey!

Step 1: Crafting the Obliteration Plane:

  1. Create a Plane:

    • In Unity's Hierarchy panel, right-click and choose 3D Object -> Plane.

  • Position the plane behind the player to act as the Cube Obliteration Zone. You can do this by adjusting the Transform component's position values in the Inspector window.

  1. Tagging the Plane:

    • Create a new tag by going to Edit -> Project Settings -> Tags and Layers.

    • In the Tags section, click the "+" button, add a new tag "Cube", and press Enter.

  2. Apply the Tag to the Plane:

    • Select the plane in the Hierarchy panel.

    • In the Inspector window, find the "Tag" field in the "Tag" component and assign the "Cube" tag to the plane.

  3. Adjust Collider (Optional):

    • If the plane doesn't have a Collider component, consider adding one. This ensures that the Cube Killer script can detect collisions.

Step 2: Cube Killer Script Explanation:

Now that our Obliteration Plane is ready, let's understand the Cube Killer script, responsible for cleaning up the cubes that dare to touch the forbidden surface.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeKiller : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Cube") // Check if the entering object has the "Cube" tag
        {
            Destroy(other.transform.parent.gameObject); // Destroy the parent object (assumed to be the cube)
        }
    }
}
  • OnTriggerEnter: This method is called when a GameObject enters the trigger collider of the Cube Killer object.

  • if (other.tag == "Cube"): Checks if the entering object has the tag "Cube." This ensures that only cubes trigger the destruction process.

  • Destroy(other.transform.parent.gameObject): Destroys the parent object of the collider, assuming the cube is a child of another GameObject. Adjust this based on your hierarchy.

Step 3: Integrate Cube Killer Script:

  1. Attach the CubeKiller script to the Obliteration Plane GameObject.

  2. Ensure the Cube Killer GameObject has a Collider component. If not, add a Collider component to detect collisions.

  3. With these steps completed, the Cube Killer script will eliminate cubes upon collision with the Obliteration Plane.

Conclusion:

Congratulations, architects of rhythm! You've now laid the groundwork for the Cube Obliteration Zone. As cubes gracefully touch the plane, they meet their rhythmic demise, bringing a satisfying conclusion to our Beat Saber VR development journey.

Happy Coding :)

ย