Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save todorok1/887f918c4b68ad7c682f1f17dd640750 to your computer and use it in GitHub Desktop.
シンプルRPGチュートリアル第6回 操作キャラクターの移動を制御するクラス
/// <summary>
/// 操作キャラクターを移動させます。
/// </summary>
/// <param name="moveDirection">移動方向</param>
/// <param name="animDirection">アニメーションの方向</param>
void MovePlayer(Vector2Int moveDirection, MoveAnimationDirection animDirection)
{
if (moveDirection == Vector2Int.zero)
{
return;
}
_isMoving = true;
// 論理的な座標から移動先の座標を計算し、ワールド座標に変換します。
_posOnTile += (Vector3Int)moveDirection;
var targetWorldPos = _baseTilemap.CellToWorld(_posOnTile);
targetWorldPos += _baseTilemap.tileAnchor;
// 移動方向に応じたアニメーションに切り替えます。
if (_animator != null)
{
_animator.SetInteger(AnimationSettings.DirectionParameterName, (int)animDirection);
}
// コルーチンを使ってキャラクターのゲームオブジェクトを移動させます。
var sourcePos = gameObject.transform.position;
StartCoroutine(MovePlayerProcess(sourcePos, targetWorldPos));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment