#This surprisingly crashes my game
1 messages · Page 1 of 1 (latest)
yes — later tonight when i’m back home. thanks for the follow up
private void Start()
{
transform.LookAt(orb.transform.position);
SpawnEnergy();
}
private async void SpawnEnergy()
{
while (true)
{
// Cooldown
try {
await Task.Delay(Random.Range(delayMin, delayMax), destroyCancellationToken);
}
catch { }
// Trajectory
Vector3 locationOffset = Random.insideUnitCircle.normalized;
Vector3 location = transform.position + locationOffset * spawnRadius;
Vector3 direction = (transform.position - location).normalized;
Vector3 force = direction * Random.Range(speedMin, speedMax);
// Creation
GameObject orbGameobject = Instantiate(orbPrefab, location, Quaternion.identity);
Rigidbody orbRigidbody = orbGameobject.GetComponent<Rigidbody>();
orbRigidbody.AddForce(force, ForceMode.Impulse);
orbRigidbody.AddTorque(Random.insideUnitCircle * Random.Range(0.25f, 1.5f));
}
}
It crashes when I get the TaskCancelledException, so when I stop running the game
I'm using Unity 2022.3.11f1