UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a falling cube"
using UnityEngine; public class FallingCube : MonoBehaviour { public GameObject cubePrefab; public float spawnInterval = 1f; private float timeSinceLastSpawn; // Start is called before the first frame update void Start() { timeSinceLastSpawn = 0f; } // Update is called once per frame void Update() { timeSinceLastSpawn += Time.deltaTime; if (timeSinceLastSpawn >= spawnInterval) { SpawnFallingCube(); timeSinceLastSpawn = 0f; } } void SpawnFallingCube() { // Instantiate a new cube at a random position above the scene Vector3 spawnPosition = new Vector3(Random.Range(-5f, 5f), 10f, Random.Range(-5f, 5f)); GameObject newCube = Instantiate(cubePrefab, spawnPosition, Quaternion.identity); // Add a Rigidbody component to the cube to make it fall due to gravity newCube.AddComponent<Rigidbody>(); } }