Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created April 6, 2025 08:17
Show Gist options
  • Select an option

  • Save todorok1/c42560115f08f10a7a8b4e885f2f8da2 to your computer and use it in GitHub Desktop.

Select an option

Save todorok1/c42560115f08f10a7a8b4e885f2f8da2 to your computer and use it in GitHub Desktop.
シンプルRPGチュートリアル第62回 キャラクターの移動を行うクラスの基底クラス
/// <summary>
/// キャラクターを移動させるコルーチンです。
/// </summary>
/// <param name="sourcePos">移動元の位置</param>
/// <param name="targetPos">移動先の位置</param>
protected IEnumerator MovePlayerProcess(Vector3 sourcePos, Vector3 targetPos)
{
// 完了後の時間を算出して、それまでの間、毎フレームLerpメソッドで位置を計算します。
var animFinishTime = Time.time + _moveTime;
var startedTime = Time.time;
var pausedTime = 0.0f;
while (Time.time < animFinishTime)
{
if (_isMovingPaused)
{
pausedTime += Time.deltaTime;
animFinishTime += Time.deltaTime;
yield return null;
continue;
}
// 開始時刻からの経過時間を計算します。移動が停止している場合は経過時間から除外します。
var elapsedTime = Time.time - startedTime - pausedTime;
var rate = Mathf.Clamp01(elapsedTime / _moveTime);
Vector3 pos = Vector3.Lerp(sourcePos, targetPos, rate);
gameObject.transform.position = pos;
yield return null;
}
// 最終的な位置を設定して移動中フラグをfalseにします。
gameObject.transform.position = targetPos;
_isMoving = false;
PostMove();
}
/// <summary>
/// キャラクター移動後の処理です。
/// </summary>
protected virtual void PostMove()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment