アニメーションのフレームから時間を計算するように変更

This commit is contained in:
kimura 2022-02-10 11:34:06 +09:00
parent 3282eb745e
commit bec80c9c43
1 changed files with 22 additions and 9 deletions

View File

@ -16,7 +16,6 @@ namespace MyGame.Scenes.Main.Scripts
[SerializeField] private int heartCount = 5; [SerializeField] private int heartCount = 5;
[SerializeField] private float minCurve = 100f; [SerializeField] private float minCurve = 100f;
[SerializeField] private float maxCurve = 400f; [SerializeField] private float maxCurve = 400f;
[SerializeField] private float waitTime = .3f;
[SerializeField] private float curveTime = .5f; [SerializeField] private float curveTime = .5f;
[SerializeField] private float linerTime = .5f; [SerializeField] private float linerTime = .5f;
private readonly Subject<Unit> effectCompleteSubject = new Subject<Unit>(); private readonly Subject<Unit> effectCompleteSubject = new Subject<Unit>();
@ -39,7 +38,10 @@ namespace MyGame.Scenes.Main.Scripts
{ {
var coin = Instantiate(coinPrefab, beginPos, Quaternion.identity, transform); var coin = Instantiate(coinPrefab, beginPos, Quaternion.identity, transform);
coin.GetComponent<Animator>()?.Play("BulkOrde_EarnedCoins"); coin.GetComponent<Animator>()?.Play("BulkOrde_EarnedCoins");
Effect(coin, goalPos, () => effectCompleteSubject.OnNext(Unit.Default)); Observable.NextFrame().Subscribe(_ =>
{
Effect(coin, goalPos, () => effectCompleteSubject.OnNext(Unit.Default));
}).AddTo(this);
}); });
} }
} }
@ -48,8 +50,8 @@ namespace MyGame.Scenes.Main.Scripts
{ {
// 最後のeffect終了時か、このオブジェクトが破棄された時に度だけonCompleteが実行される // 最後のeffect終了時か、このオブジェクトが破棄された時に度だけonCompleteが実行される
effectCompleteSubject.Take(heartCount).Last() effectCompleteSubject.Take(heartCount).Last()
.Amb(this.OnDestroyAsObservable()) .Amb(this.OnDestroyAsObservable())
.Subscribe(_ => { }, () => .Subscribe(_ => { }, () =>
{ {
onComplete?.Invoke(); onComplete?.Invoke();
}); });
@ -65,12 +67,19 @@ namespace MyGame.Scenes.Main.Scripts
private void Effect(Transform target, Vector3 goalPos, Action onComplete = null) private void Effect(Transform target, Vector3 goalPos, Action onComplete = null)
{ {
// localPosじゃなくて worldでやりたい
var animator = target.GetComponent<Animator>(); var animator = target.GetComponent<Animator>();
var nameHash = animator.GetCurrentAnimatorStateInfo(0).shortNameHash;
var clip = animator.GetCurrentAnimatorClipInfo(0)[0].clip;
var beginDuration = 37 / clip.frameRate;
var beginNormalizedTime = beginDuration / clip.length;
var endNormalizedTime = 52 / clip.frameRate / clip.length;
var endDuration = (1 - endNormalizedTime) * clip.length;
var direction = Random.value <= .5f ? -1 : 1; var direction = Random.value <= .5f ? -1 : 1;
var depth = Random.Range(minCurve, maxCurve); var depth = Random.Range(minCurve, maxCurve);
this.CallWaitForSeconds(waitTime, () => this.CallWaitForSeconds(beginDuration, () =>
{ {
animator.Play(nameHash, 0, beginNormalizedTime);
animator.speed = 0f; animator.speed = 0f;
var initPos = target.localPosition; var initPos = target.localPosition;
this.CallLerp(curveTime, f => this.CallLerp(curveTime, f =>
@ -79,15 +88,19 @@ namespace MyGame.Scenes.Main.Scripts
target.localPosition = initPos + new Vector3(f * direction * depth, x * x * depth - depth); target.localPosition = initPos + new Vector3(f * direction * depth, x * x * depth - depth);
}, () => }, () =>
{ {
var pos = target.position;
this.CallLerp(linerTime, f => this.CallLerp(linerTime, f =>
{ {
var pos = target.position;
target.position = Vector2.Lerp(pos, goalPos, f); target.position = Vector2.Lerp(pos, goalPos, f);
}, () => }, () =>
{ {
animator.speed = 1f; animator.speed = 1f;
onComplete?.Invoke(); animator.Play(nameHash, 0, endNormalizedTime);
Destroy(target.gameObject, 5f); this.CallWaitForSeconds(endDuration, () =>
{
onComplete?.Invoke();
Destroy(target.gameObject);
});
}); });
}); });
}); });