#14 Spawning Symmetry: Duplicating Red Cubes and Harmonizing the Beat
Introduction:
Greetings, rhythm architects! In this segment of our Beat Saber VR series, we're delving into the rhythmic realm of duplicating and synchronizing red cubes. Join us as we amplify the visual and auditory experience by multiplying the presence of red cubes, ensuring that every beat is a symphony of vibrant energy and synchronized spawns.
Step 1: Duplicating the RedCube Prefab:
Locate Cube Prefab:
Head to your project assets and find the Cube prefab.
Duplicate the Prefab:
Right-click on the Cube prefab and select "Duplicate." If you are not able to find "Duplicate", just Copy and Paste.
Rename the duplicated prefab if needed.
Adjust Properties:
Fine-tune the properties of the duplicated RedCube prefab.
Ensure that the color, size, and any specific characteristics align with your visual aesthetic.
Step 2: Synchronizing RedCube Spawning:
Update CubeSpawner Logic:
Enhance the CubeSpawner script to spawn red cubes in synchronization with blue cubes.
Consider adjusting spawn patterns or frequencies to maintain a balanced rhythm.
COPY
public GameObject cube;
public Vector3 spawnOffset = new Vector3(0.0f, 1.5f, 1.0f);
public float xRange = 0.35f;
public float yRange = 0.4f;
public float minDistance = 12f; // Minimum distance from the player
public float minSpawnDelay = 1.5f;
public float maxSpawnDelay = 2.0f;
private float lastSpawnTime;
// Start is called before the first frame update
void Start()
{
lastSpawnTime = Time.time;
}
// Update is called once per frame
void Update()
{
if (CanSpawnCube())
{
SpawnCube();
}
}
bool CanSpawnCube()
{
float elapsedTimeSinceLastSpawn = Time.time - lastSpawnTime;
float randomSpawnDelay = UnityEngine.Random.Range(minSpawnDelay, maxSpawnDelay);
return elapsedTimeSinceLastSpawn > randomSpawnDelay;
}
void SpawnCube()
{
// Randomize X and Y positions
float x = UnityEngine.Random.Range(-xRange, xRange);
float y = UnityEngine.Random.Range(-yRange / 4.0f, yRange);
// Calculate spawn position
Vector3 spawnPos = transform.position - new Vector3(0.0f, 0.0f, spawnOffset.z);
// Calculate direction towards the player
Vector3 directionToPlayer = (transform.position - spawnPos).normalized;
// Adjust initial position based on direction and distance
Vector3 initialPos = new Vector3(x, y, 0.0f) + directionToPlayer * minDistance;
// Spawn Cube
GameObject go = Instantiate(cube, initialPos, Quaternion.identity);
go = go.transform.Find("Cube").gameObject;
// Get Rigidbody
Rigidbody rb = go.GetComponent<Rigidbody>();
rb.useGravity = false;
// Calculate random velocity direction
float theta = UnityEngine.Random.Range((float)(Math.PI / 4.0f), (float)(3.0f * Math.PI / 4.0f));
float mag = UnityEngine.Random.Range(1.0f, 3.0f);
// Calculate added velocity
Vector3 addedVelocity = -directionToPlayer * mag;
// Apply force to move towards the player
rb.AddForce(addedVelocity, ForceMode.VelocityChange);
// Adjust cube scale
float scale = UnityEngine.Random.Range(0.1f, 0.25f);
go.transform.localScale = new Vector3(scale, scale, scale);
// Update last spawn time
lastSpawnTime = Time.time;
}
Step 3: Update CubeKiller Script:
add the tag for "RedCube" and add that to the script
COPY
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeKiller : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Cube" || other.tag == "RedCube") // when these cubes touches the plane the gameObjects gets destroyed
{
// Debug.Log("Inside OnTriggerEnter");
Destroy(other.transform.parent.gameObject);
}
}
}
Step 4: Testing and Refinement:
Playtest the Scene:
Enter Play mode in Unity and test the duplication of red cubes and lightsabers.
Ensure that the visual and auditory elements align seamlessly with the rhythm.
Refine as Needed:
Fine-tune spawn rates, positions, and interactions based on playtesting feedback.
Strive for a symphonic balance between blue and red elements.
Conclusion:
With the duplication and synchronization of red cubes, your Beat Saber VR realm is now a canvas of vibrant beats and dynamic spawns. Players are in for an immersive experience where every beat pulses with energy and symmetry.
Happy Coding :)