I am trying to modify a little bit Spawn Objects Over Time from here: https://engine.needle.tools/docs/scripting-examples.html#spawn-objects-over-time
Firstly it demanded to use asset reference, because serializable didnt work well, now it says "instance.clone() is not a function" i tride to use Object3D but error was the same
My code looks like this:
import { AssetReference, Behaviour, GameObject, LogType, serializeable, showBalloonMessage, WaitForSeconds } from "@needle-tools/engine";
import { Event, Object3D, Vector3 } from "three";
export class TimedSpawn extends Behaviour {
@serializeable(AssetReference)
object!: GameObject;
interval: number = 1000;
max: number = 100;
spawnRange: Vector3 = new Vector3(10, 10, 10); // Default range is (-10 to 10) for x, y, z
private spawned: number = 0;
awake() {
if (!this.object) {
console.warn("TimedSpawn: no object to spawn");
showBalloonMessage("TimedSpawn: no object to spawn", LogType.Warn);
return;
}
GameObject.setActive(this.object, false);
this.startCoroutine(this.spawn())
}
*spawn() {
if (!this.object) return;
while (this.spawned < this.max) {
const instance = GameObject.instantiate(this.object);
const randomPos = this.getRandomPosition();
instance!.transform.position.x = randomPos.x;
instance!.transform.position.y = randomPos.y;
instance!.transform.position.z = randomPos.z;
GameObject.setActive(instance!, true);
this.spawned += 1;
yield WaitForSeconds(this.interval / 1000);
}
}
getRandomPosition(): Vector3 {
const randomX = Math.random() * this.spawnRange.x * 2 - this.spawnRange.x;
const randomY = Math.random() * this.spawnRange.y * 2 - this.spawnRange.y;
const randomZ = Math.random() * this.spawnRange.z * 2 - this.spawnRange.z;
return new Vector3(randomX, randomY, randomZ);
}
}
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development, and can be deployed anywhere. It is flexible, extensible, and collaboration and XR come naturally. Needle Exporter for Unity bridges the Unity Editor and the web runtime. It helps you to export your assets, animations, lightmaps and so on to the web. It is...