Skip to content

Commit 35b41b5

Browse files
committed
Adds a TrailsManager to reset all trails after change
1 parent b1ee7db commit 35b41b5

File tree

6 files changed

+157
-13
lines changed

6 files changed

+157
-13
lines changed

Assets/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:0df15d5ec15d7f5dfa05bddcb00641eafd780067458f6d16d6b98ce2b2f81c29
3-
size 34013
2+
oid sha256:e8c4dc29b13742f215ac9019934bd60b398de58500577a38593b89b40da79ec7
3+
size 19272

Assets/_Project/Scripts/Effects/TrailController.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class TrailController : MonoBehaviour {
66

7-
public TrailRenderer trail;
7+
public TrailRenderer trailRenderer;
88
public int siblingCount;
99
public int trailIndex;
1010
public float trailWidth;
@@ -23,17 +23,27 @@ public class TrailController : MonoBehaviour {
2323
};
2424

2525

26+
2627
private void Awake ()
2728
{
2829
// get renderer
29-
trail = GetComponent<TrailRenderer> ();
30+
trailRenderer = GetComponent<TrailRenderer> ();
31+
}
32+
33+
34+
35+
36+
public void Init ()
37+
{
38+
//Debug.Log ("Init() called on TrailController");
39+
3040

3141
// get random color string
3242
trailColor = colors [(int)Random.Range (0, colors.Length - 1)];
3343
// if hex parses
3444
if (ColorUtility.TryParseHtmlString ("#" + trailColor + "FF", out color)) {
3545
// change material color
36-
trail.material.color = color;
46+
trailRenderer.material.color = color;
3747
}
3848

3949
// number of siblings (other trails)
@@ -48,16 +58,15 @@ private void Awake ()
4858
float positionRange = positionMax - positionMin;
4959

5060
// sets positionsByIndex array by number of trails
51-
positionsByIndex = new float[siblingCount];
61+
positionsByIndex = new float [siblingCount];
5262

5363
// calculate range between each trail
5464
float trailsRange = positionRange / (siblingCount);
5565

5666
// place trails so the outer edges are touching positionMin and positionMax
57-
for (int i = 0; i < siblingCount; i++)
58-
{
67+
for (int i = 0; i < siblingCount; i++) {
5968
// starts with positionMin plus an offset that lets the trail edge touch it
60-
positionsByIndex[i] = positionMin + (trailsRange * (i + 0.5f)); // the 0.5 places it in the middle of the range, or something
69+
positionsByIndex [i] = positionMin + (trailsRange * (i + 0.5f)); // the 0.5 places it in the middle of the range, or something
6170
} // tbh I don't really know, I'm just glad it works
6271

6372
// set the trail width to the range plus a 'lil extra to cover the background
@@ -68,12 +77,12 @@ private void Awake ()
6877
transform.localPosition = new Vector3 (0, positionsByIndex [trailIndex], 0);
6978

7079
// set w & h
71-
trail.startWidth = trailWidth; // 5 = 0.45f
72-
trail.endWidth = trailWidth;
80+
trailRenderer.startWidth = trailWidth; // 5 = 0.45f
81+
trailRenderer.endWidth = trailWidth;
7382

7483
// set vertices
75-
trail.numCornerVertices = 10;
76-
trail.numCapVertices = 5;
84+
trailRenderer.numCornerVertices = 10;
85+
trailRenderer.numCapVertices = 5;
7786

7887
}
7988

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class TrailsManager : MonoBehaviour {
6+
7+
public int minTrailCount = 3;
8+
public int maxTrailCount = 10;
9+
public int numberTrails = 0;
10+
public GameObject trailPrefab;
11+
public Dictionary<string, GameObject> trailDict;
12+
13+
private void Awake ()
14+
{
15+
trailDict = new Dictionary<string, GameObject> ();
16+
17+
// TEMP ADD RANDOM COUNT
18+
numberTrails = (int)Random.Range (minTrailCount, maxTrailCount);
19+
for (int i = 0; i < numberTrails; i++) {
20+
AddTrail (i, false);
21+
}
22+
// update after a second
23+
StartCoroutine (UpdateTrails (1f));
24+
}
25+
26+
27+
/**
28+
* Adds the trail at index
29+
*/
30+
void AddTrail (int index, bool updateAll)
31+
{
32+
// add player to scene
33+
GameObject obj = (GameObject)Instantiate (trailPrefab);
34+
// create name for trail obj
35+
string name = "trail-" + (index + 1).ToString ();
36+
// set name in Unity Editor
37+
obj.name = name;
38+
// parent under this manger
39+
obj.transform.parent = gameObject.transform;
40+
// if it already exists
41+
if (trailDict.ContainsKey (name)) {
42+
// update
43+
//trailDict.Add (name, obj);
44+
} else {
45+
// add to dict
46+
trailDict.Add (name, obj);
47+
}
48+
49+
50+
if (updateAll) StartCoroutine (UpdateTrails (1f));
51+
}
52+
53+
54+
/**
55+
* Loop through all the trails to update their positions after trails added or removed
56+
*/
57+
IEnumerator UpdateTrails (float wait)
58+
{
59+
yield return new WaitForSeconds (wait);
60+
61+
foreach (string t in trailDict.Keys) {
62+
trailDict [t].GetComponent<TrailController> ().Init ();
63+
}
64+
65+
}
66+
67+
68+
69+
}

Assets/_Project/Scripts/Effects/TrailsManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/packages-lock.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
{
22
"dependencies": {
3+
"com.unity.2d.animation": {
4+
"version": "4.2.6",
5+
"depth": 1,
6+
"source": "registry",
7+
"dependencies": {
8+
"com.unity.2d.common": "3.0.1",
9+
"com.unity.mathematics": "1.1.0",
10+
"com.unity.2d.sprite": "1.0.0",
11+
"com.unity.modules.animation": "1.0.0",
12+
"com.unity.modules.uielements": "1.0.0"
13+
},
14+
"url": "https://packages.unity.com"
15+
},
16+
"com.unity.2d.common": {
17+
"version": "3.0.1",
18+
"depth": 1,
19+
"source": "registry",
20+
"dependencies": {
21+
"com.unity.2d.sprite": "1.0.0",
22+
"com.unity.modules.uielements": "1.0.0"
23+
},
24+
"url": "https://packages.unity.com"
25+
},
26+
"com.unity.2d.psdimporter": {
27+
"version": "3.1.6",
28+
"depth": 0,
29+
"source": "registry",
30+
"dependencies": {
31+
"com.unity.2d.common": "3.0.1",
32+
"com.unity.2d.animation": "4.2.6",
33+
"com.unity.2d.sprite": "1.0.0"
34+
},
35+
"url": "https://packages.unity.com"
36+
},
37+
"com.unity.2d.sprite": {
38+
"version": "1.0.0",
39+
"depth": 1,
40+
"source": "builtin",
41+
"dependencies": {}
42+
},
343
"com.unity.collab-proxy": {
444
"version": "1.3.8",
545
"depth": 0,
@@ -37,6 +77,13 @@
3777
"dependencies": {},
3878
"url": "https://packages.unity.com"
3979
},
80+
"com.unity.mathematics": {
81+
"version": "1.1.0",
82+
"depth": 2,
83+
"source": "registry",
84+
"dependencies": {},
85+
"url": "https://packages.unity.com"
86+
},
4087
"com.unity.render-pipelines.core": {
4188
"version": "8.2.0",
4289
"depth": 1,

0 commit comments

Comments
 (0)