Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ProjectileSpammer.cs
Go to the documentation of this file.
1 using UnityEngine;
2 
3 namespace droid.Runtime.Utilities.Sampling {
4  public class ProjectileSpammer : MonoBehaviour {
5  [SerializeField] string _assigned_tag = "Obstruction";
6  float _last_spawn = 0f;
7  [SerializeField] [Range(.0f, 3.0f)] float _life_time = 2f;
8  [SerializeField] Vector2 _mass_range = new Vector2(1f, 4f);
9  [SerializeField] float _projectile_multiplier = 100f;
10  [SerializeField] [Range(.0f, 1.0f)] float _scale_modifier = 0.2f;
11  [SerializeField] float _spawn_radius = 20f;
12  [SerializeField] float _spawn_rate = 0.5f;
13  [SerializeField] Transform _target = null;
14 
15  void Update() {
16  if (this._last_spawn + 1 / this._spawn_rate < Time.time) {
17  if (this._spawn_rate > 1) {
18  for (var i = 0; i < this._spawn_rate; i++) {
19  this.SpawnRandomProjectile();
20  }
21  } else {
22  this.SpawnRandomProjectile();
23  }
24  }
25  }
26 
27  void SpawnRandomProjectile() {
28  var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
29  cube.tag = this._assigned_tag;
30  cube.transform.position = this._target.transform.position + Random.onUnitSphere * this._spawn_radius;
31  cube.transform.rotation = Random.rotation;
32  cube.transform.localScale = (Vector3.one - Random.insideUnitSphere) / 2 * this._scale_modifier;
33  var rb = cube.AddComponent<Rigidbody>();
34  rb.AddForce((this._target.position - cube.transform.position) * this._projectile_multiplier);
35  rb.AddTorque(Random.insideUnitSphere);
36  rb.mass = Random.Range(this._mass_range.x, this._mass_range.y);
37  var sf = cube.AddComponent<SelfDestruct>();
38  sf.LifeTime = this._life_time;
39  }
40  }
41 }