-
-
Save todorok1/c5a142db21c9175a3a462e9f805f3943 to your computer and use it in GitHub Desktop.
シンプルRPGチュートリアル第44回 キャラクターの移動を行うクラスの基底クラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// キャラクターを移動させます。 | |
/// </summary> | |
/// <param name="moveDirection">移動方向</param> | |
/// <param name="animDirection">アニメーションの方向</param> | |
protected virtual void MoveCharacter(Vector2Int moveDirection, MoveAnimationDirection animDirection) | |
{ | |
if (moveDirection == Vector2Int.zero) | |
{ | |
return; | |
} | |
// 移動機能が一時停止されている場合は処理を抜けます。 | |
if (_isMovingPaused) | |
{ | |
return; | |
} | |
// 移動方向に応じたアニメーションに切り替えます。 | |
if (_animator != null) | |
{ | |
_animator.SetInteger(AnimationSettings.DirectionParameterName, (int)animDirection); | |
} | |
// 中略 | |
} | |
/// <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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment