Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created January 1, 2025 13:33
Show Gist options
  • Select an option

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

Select an option

Save todorok1/fb8d5062d0a612a19a878601be6b68d7 to your computer and use it in GitHub Desktop.
シンプルRPGチュートリアル第6回 操作キャラクターの移動を制御するクラス
/// <summary>
/// 操作キャラクターを移動させるコルーチンです。
/// </summary>
/// <param name="sourcePos">移動元の位置</param>
/// <param name="targetPos">移動先の位置</param>
IEnumerator MovePlayerProcess(Vector3 sourcePos, Vector3 targetPos)
{
// 完了後の時間を算出して、それまでの間、毎フレームLerpメソッドで位置を計算します。
var animFinishTime = Time.time + _moveTime;
var startedTime = Time.time;
while (Time.time < animFinishTime)
{
var elapsedTime = Time.time - startedTime;
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment