47 lines
1.2 KiB
C#

using UnityEngine;
[RequireComponent (typeof(BezierRunner))]
public class Drone : MonoBehaviour
{
[SerializeField] AmmoBox box;
[SerializeField, Range(0, 1)] float progressToDrop = 0.5f;
[SerializeField] AudioSource source;
[SerializeField] AnimationCurve volumeCurve;
[SerializeField] AnimationCurve pitchCurve;
private BezierRunner runner;
private bool dropped = false;
public void Init(BezierWay bezierWay)
{
runner = GetComponent<BezierRunner>();
runner.SetBezierWay(bezierWay);
runner.SetProgress(0);
runner.Initialize();
runner.OnReachedEnd += DestroyYourself;
}
private void Update()
{
bool needDrop = !dropped && runner.CurrentProgress >= progressToDrop;
if (needDrop) DropBox();
source.volume = volumeCurve.Evaluate(runner.CurrentProgress);
source.pitch = pitchCurve.Evaluate(runner.CurrentProgress);
}
private void DropBox()
{
box.transform.parent = null;
box.Configure();
dropped = true;
}
private void DestroyYourself()
{
runner.OnReachedEnd -= DestroyYourself;
Destroy(gameObject);
}
}