メインコンテンツまでスキップ

roulette_judge

例えば、ルーレットの表面に特定のセクションごとの角度を使用して、停止位置を判定。

例:セクションごとの停止位置を判定する方法

  1. ルーレットにセクションを割り当てる:ルーレットを円形セクションに分割し、各セクションに特定の「目」や「結果」を割り当てます。
  2. 回転角度を取得して停止位置を判定するtransform.rotationを使って回転の最終的な角度を取得し、それを基にどのセクションで止まったかを計算します。

コード例

using UnityEngine;

public class RouletteSpinner : MonoBehaviour
{
public float initialSpeed = 500f; // 初期の回転速度
public float deceleration = 50f; // 減速率
public float stopThreshold = 0.1f; // 回転を停止させる閾値
public int numberOfSections = 4; // ルーレットのセクションの数
public string[] sectionNames; // セクションごとの名前や結果

private float currentSpeed;
private bool isSpinning = false;

void Start() {
// ルーレットの回転速度を初期化
currentSpeed = initialSpeed;

// セクション名を指定(例として8セクション)
if (sectionNames == null || sectionNames.Length != numberOfSections) {
sectionNames = new string[numberOfSections];
for (int i = 0; i < numberOfSections; i++) {
sectionNames[i] = "Section " + (i + 1); // デフォルトのセクション名
}
}
}

void Update() {
if (isSpinning) {
// Z軸周りに時計回りで回転
transform.Rotate(Vector3.forward, -currentSpeed * Time.deltaTime);

// 徐々に減速させる
currentSpeed -= deceleration * Time.deltaTime;

// 回転が停止閾値を下回ったら停止
if (currentSpeed <= stopThreshold) {
currentSpeed = 0f;
isSpinning = false;
Debug.Log("Roulette stopped.");

// 停止位置を判定
DetermineStopPosition();
}
}

// スペースキーで回転を開始
if (Input.GetKeyDown(KeyCode.Space) && !isSpinning) {
StartSpin();
}
}

// 回転を開始するメソッド
void StartSpin() {
isSpinning = true;
currentSpeed = initialSpeed; // 初期速度で再び回転を開始
Debug.Log("Roulette started spinning.");
}

// 停止位置を判定するメソッド
void DetermineStopPosition() {
// Z軸周りの現在の回転角度を取得(ルーレットの回転軸に依存)
float zRotation = transform.eulerAngles.z;

// セクションの角度(360度をセクション数で割る)
float sectionAngle = 360f / numberOfSections;

// 現在の回転角度からセクションを判定
int stoppedSection = Mathf.FloorToInt(zRotation / sectionAngle);

// 判定されたセクションの情報を表示
Debug.Log("Stopped at: " + sectionNames[stoppedSection]);
}
}

解説

  1. セクションの数と名前の定義:

    • numberOfSections でルーレットのセクション数を指定(例:8セクション)。
    • sectionNames で各セクションに名前や結果(「1」「2」「3」や「赤」「黒」など)を割り当てます。
  2. 回転角度の取得:

    • ルーレットの回転角度は transform.eulerAngles.z で取得します。これによりZ軸周りの回転角度(0〜360度)が得られます。
  3. セクションの角度計算:

    • 360f / numberOfSections で各セクションの角度を計算します(例えば、8セクションなら1セクションは45度)。
  4. 停止位置の判定:

    • 現在の回転角度をセクションの角度で割り、Mathf.FloorToInt を使用してどのセクションで止まったかを整数で判定します。
  5. 停止したセクションを表示:

    • 判定されたセクション番号に基づいて、対応する sectionNames をコンソールに表示します。

例:

  • もしルーレットが停止した角度が 120度 で、セクションの角度が 45度 だとすると、120度 / 45度 = 2.67 で、小数点以下を切り捨てて「セクション2」で止まったと判定されます。

これにより、ルーレットがどのセクションで停止したかを判定し、何が当たったのかを表示することができます。