短くて単純ですが、なぜこれがそのように機能するのかわかりません。
using UnityEngine;
using System.Collections.Generic;
// this struct holds data for a single 'shake' source
public struct Shake
{
public float AmountX;
public float AmountY;
public float Duration;
private float percentageComplete;
public float PercentageComplete { get { return percentageComplete; } }
public Shake(float _amountX, float _amountY, float _duration)
{
AmountX = _amountX;
AmountY = _amountY;
Duration = _duration;
percentageComplete = 0f;
Debug.Log("wtf");
}
public void Update()
{
Debug.Log("a " + percentageComplete);
percentageComplete += Time.deltaTime;
Debug.Log("b " + percentageComplete);
AmountX = Mathf.Lerp(AmountX, 0f, percentageComplete);
AmountY = Mathf.Lerp(AmountY, 0f, percentageComplete);
}
}
// This class uses that data to implement the shakes on a game camera
public class CameraShake : MonoBehaviour
{
// components
private Transform myTransform;
// vars
private List<Shake> shakes;
private float totalShakeX;
private float totalShakeY;
private void Awake()
{
myTransform = transform;
shakes = new List<Shake>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
AddShake(new Shake(.1f, .1f, 1f));
}
totalShakeX = 0f;
totalShakeY = 0f;
for (int i = 0; i < shakes.Count; i++)
{
shakes[i].Update();
Debug.Log("c " + shakes[i].PercentageComplete);
totalShakeX += shakes[i].AmountX;
totalShakeY += shakes[i].AmountY;
if (shakes[i].PercentageComplete >= 1f)
{
shakes.RemoveAt(i);
i--;
}
}
myTransform.position += new Vector3(totalShakeX, 0f, totalShakeY);
}
public void AddShake(Shake shake)
{
shakes.Add(shake);
}
}
(Unity3Dゲームエンジンを使用して)このコードを実行すると、構造体Shakeが正しい値をログに記録しません。
完了率は、これらの値(フレームごと)をログに記録します。a0 b 0.001(または、ここでは、そのTime.deltaTime、前のフレームの処理にかかった時間の長さ)c 0
何が得られますか?なぜpercentageCompleteがフレームごとに0にリセットされるのですか?
Shakeを構造体ではなくクラスに変更すると、正常に機能します。