I am trying to modify a little bit Spawn Objects Over Time from here: Script Examples | Needle Engine Documentation
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);
}
you can also always download the sample projects Needle Engine Samples and look at one of the scenes that has a feature similar to what you want - the code is completely available
This one works, but only for one time, and it ignores current position of the spawner, I tried everything, including chatGPT, but cannot find where I am wrong. I just need spawn object periodically in certain location, with some random coordinate drift
import { Vector3 } from "three";
export class TimedSpawn extends Behaviour {
@serializeable(AssetReference)
object!: AssetReference;
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;
}
// Start the spawn coroutine
this.startCoroutine(this.spawn());
}
*spawn() {
while (this.spawned < this.max) {
// Use yield to handle the promise
const instance = yield this.object!.instantiate();
if (!instance) return;
const spawnerPos = this.gameObject.transform.position; // Get the spawner's current position
const randomPos = this.getRandomPosition();
// Adjust the spawned object's position by adding the spawner's position to the random offset
instance.transform.position.x = spawnerPos.x + randomPos.x;
instance.transform.position.y = spawnerPos.y + randomPos.y;
instance.transform.position.z = spawnerPos.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);
}
}```
*by user 697872771497328640*