Unityで学ぶ:カメラをゆっくり回転させる方法

Unityで回転処理を処理を記載するの毎回調べるので、メモ書きで書きます。

using System.Collections;
using UnityEngine;

public class CameraRotator : MonoBehaviour
{
    // 回転速度
    public float rotationSpeed = 1f;

    // ターゲットに到達したかどうかを示すフラグ
    private bool isAtTarget = false;

    // カメラをゆっくり回転させるコルーチン
    IEnumerator RotateCameraSlowly()
    {
        float totalRotation = 0f;
        float targetRotation = 35f; // 目標の回転角度

        while (totalRotation < targetRotation)
        {
            float currentRotation = -rotationSpeed;
            Camera.main.transform.Rotate(currentRotation, 0, 0);
            totalRotation += Mathf.Abs(currentRotation);

            yield return null;
        }

        // ターゲットに到達したことを示すフラグをセット
        isAtTarget = true;
    }

    // 他のメソッドやイベントからこのコルーチンを呼び出す例
    void Start()
    {
        StartCoroutine(RotateCameraSlowly());
    }

}
 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


最近のコメント