-
Notifications
You must be signed in to change notification settings - Fork 2
/
Twig.cs
51 lines (50 loc) · 1.63 KB
/
Twig.cs
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
43
44
45
46
47
48
49
50
51
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Global;
using static Extension;
public class Twig : MonoBehaviour
{
[HideInInspector] public LineRenderer line;
[HideInInspector] public float minWidth = .02f;
[HideInInspector] public float thickness = 1f;
[HideInInspector] public Vector3 destination;
[HideInInspector] public float startTime;
[HideInInspector] public float endTime;
[HideInInspector] public float maxWidth;
[HideInInspector] public int i;
private float duration;
public Twig parent = null;
private Renderer renderer;
private Vector3 source;
private Vector3 startOffset = Vector3.zero;
public Vector3 endOffset = Vector3.zero;
private float length;
void Start()
{
float depth = (timeLimit[i]-endTime)/timeLimit[i];
line = GetComponent<LineRenderer>();
thickness = Mathf.Max(maxWidth*depth, minWidth);
source = line.GetPosition(0);
duration = endTime - startTime;
renderer = GetComponent<Renderer>();
parent = gameObject.transform.parent.GetComponent<Twig>();
length = (destination - source).magnitude;
}
private float progress = 0;
void Update()
{
renderer.enabled = startTime <= time[i];
startOffset = parent?.endOffset ?? startOffset;
line.SetPosition(0, source + startOffset);
determineGrowth();
}
void determineGrowth()
{
progress = Mathf.Clamp((time[i] - startTime) / duration, 0, 1);
line.SetPosition(1, Vector3.Lerp(source, destination + endOffset, progress));
float currentWidth = Mathf.Lerp(line.startWidth, thickness, progress);
line.startWidth = currentWidth;
line.endWidth = currentWidth;
}
}