#9 Slicing Through the Beat: Implementing Blade Script for Cube Destruction in Beat Saber VR

#9 Slicing Through the Beat: Implementing Blade Script for Cube Destruction in Beat Saber VR

ยท

4 min read

Introduction:

Welcome back, fellow Beat Saber architects! In this ninth installment of our Beat Saber VR gaming blog series, we're adding a razor-sharp touch to the gameplay. Brace yourself as we delve into the intricacies of the Blade script, bringing the power to destroy cubes with every swing. Let's get started on this slicing journey!

Step 1: Import the Blade Script:

If you haven't already, create a new C# script named Blade and attach it to your Lightsaber GameObject. This script will be the maestro orchestrating the destruction of cubes upon a successful swipe.

Step 2: Define Variables and Initialization:

Let's understand the variables that set the stage for the blade's destructive performance.

public AudioClip destroySound;
private AudioSource audioSource;
private Vector3 previousPosition;
private Rigidbody bladeRigidbody;
float angleThreshold = 130.0f;
  • destroySound: The audio clip played when a cube is successfully destroyed.

  • audioSource: The AudioSource component responsible for playing the destroySound.

  • previousPosition: Keeps track of the Lightsaber's previous position for calculating the swing direction.

  • bladeRigidbody: Reference to the Lightsaber's Rigidbody component.

  • angleThreshold: A threshold angle determining successful cube destruction. Adjust as needed.

Step 3: Initialize the Script:

In the Start method, we initialize the audioSource and set the initial position of the Lightsaber.

void Start()
{
    audioSource = GetComponent<AudioSource>();
    previousPosition = transform.position;
}

Step 4: Track Lightsaber Position:

In the Update method, we continuously update the previous position of the Lightsaber.

void Update()
{
    previousPosition = transform.position;
}

Step 5: Cube Destruction on Trigger Enter:

The OnTriggerEnter method is where the magic happens. Let's dissect the steps:

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Cube")
    {
        // Check the direction of the sword swing
        Vector3 swordSwingDirection = (transform.position - previousPosition).normalized;

        // Get the direction of the arrow on the cube
        Vector3 arrowDirection = other.transform.up;

        // Get the angle between the sword swing direction and arrow direction
        float angle = Vector3.Angle(swordSwingDirection, arrowDirection);

        // Check if the angle is within the threshold
        if (angle > angleThreshold) 
        {
            // Play the destroySound
            audioSource.clip = destroySound;
            audioSource.Play();

            // Destroy the cube only if the sword is swung in the right direction
            Destroy(other.transform.parent.gameObject);
        }

        previousPosition = transform.position;
    }
}
  • swordSwingDirection: Determines the direction of the Lightsaber swing.

  • arrowDirection: Represents the direction of the arrow on the cube (adjust based on your cube orientation).

  • angle: Calculates the angle between the Lightsaber swing and the arrow direction.

  • angleThreshold: Ensures the swing angle is sufficient for cube destruction.

  • audioSource: Plays the destroySound upon successful cube destruction.

  • Destroy(other.transform.parent.gameObject): Obliterates the cube and its parent object.

Step 6: Test and Refine:

Hit play and witness the power of your Lightsaber as it slices through cubes. Fine-tune the angleThreshold to achieve the desired level of challenge.

Here's the entire code for the Blade script:

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

public class Blade : MonoBehaviour
{
    public AudioClip destroySound;
    private AudioSource audioSource;
    private Vector3 previousPosition;

    // Define a threshold angle for similarity
    float angleThreshold = 130.0f;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        previousPosition = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        previousPosition = transform.position;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Cube")
        {
            // Check the direction of the sword swing
            Vector3 swordSwingDirection = (transform.position - previousPosition).normalized;

            // Get the direction of the arrow on the cube
            Vector3 arrowDirection = other.transform.up;

            // Get the angle between the sword swing direction and arrow direction
            float angle = Vector3.Angle(swordSwingDirection, arrowDirection);

            // Check if the angle is within the threshold
            if (angle > angleThreshold) 
            {
                // Play the destroySound
                audioSource.clip = destroySound;
                audioSource.Play();

                // Destroy the cube only if the sword is swung in the right direction
                Destroy(other.transform.parent.gameObject);
            }

            previousPosition = transform.position;
        }
    }
}

Conclusion*:*

Congratulations! You've successfully implemented the Blade script, infusing your Beat Saber game with a thrilling slicing mechanic. Stay tuned for our next adventure as we explore more ways to elevate your virtual rhythm experience!

Happy Slicing :)

ย