-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoleculeSpawner.cs
More file actions
42 lines (29 loc) · 1.1 KB
/
MoleculeSpawner.cs
File metadata and controls
42 lines (29 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoleculeSpawner : MonoBehaviour
{
public Transform moleculePrefab;
public int moleculeCount = 1;
public float maxDistance = 1;
public float maxVelocity = 0;
private List<Transform> molecules;
private void spawnMolecules()
{
for (var i = 0; i < moleculeCount; i++)
{
float x = this.transform.position.x + Random.Range(-1 * maxDistance, maxDistance);
float y = this.transform.position.y + Random.Range(-1 * maxDistance, maxDistance);
float z = this.transform.position.z + Random.Range(-1 * maxDistance, maxDistance);
Transform obj = (Transform)Instantiate(moleculePrefab, new Vector3(x, y, z), Quaternion.identity);
float min = -1 * maxVelocity;
float max = maxVelocity;
Vector3 v = new Vector3(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max));
obj.GetComponent<Rigidbody>().AddForce(v, ForceMode.VelocityChange);
}
}
void Start()
{
spawnMolecules();
}
}