BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / GAME / Scripts / ComponentSpawner.cs

ComponentSpawner.cs

92128f09 History
ComponentSpawner.cs 1.3 KiB Download Raw
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class ComponentSpawner : MonoBehaviour
6{
7 [SerializeField] private GameObject componentToSpawn;
8 private float spawnDistance = 0.2f;
9 private GameObject spawned;
10
11 void Update()
12 {
13 if (spawned != null)
14 {
15 bool hasStill = false;
16 foreach (Grabbable coll in spawned.GetComponentsInChildren<Grabbable>())
17 {
18 if (Vector3.Distance(transform.position, coll.transform.position) < spawnDistance)
19 hasStill = true;
20 }
21
22 if(!hasStill)
23 {
24 /*foreach (Collider coll in spawned.GetComponentsInChildren<Collider>())
25 {
26 coll.enabled = true;
27 }*/
28
29 spawned = null;
30 }
31 }
32 else
33 {
34 spawned = Instantiate(componentToSpawn, transform.position, transform.rotation);
35 Grabbable[] grabbables = spawned.GetComponentsInChildren<Grabbable>();
36
37 foreach(Grabbable grabbable in grabbables)
38 {
39 grabbable.FreezeRigidbody();
40 }
41
42 /*foreach (Collider coll in spawned.GetComponentsInChildren<Collider>())
43 {
44 coll.enabled = false;
45 }*/
46 }
47 }
48}