(Unity6_BRP)オブジェクトを自動回転させる

(スクリプトの作成)

  • Assetsエリアの余白で右クリック
  • 「Create」→「Scripting」→「MonoBehaviour Script」をクリック

  • 名前を「AutoRotate」に変更
  • 下記のコードを書いてチェック
using UnityEngine;

public class AutoRotate : MonoBehaviour
{
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // この一行を記載する。
        transform.Rotate(new Vector3(0, 0, 90) * Time.deltaTime);
    }
}

(実行)

  • スクリプトをCoinオブジェクトに追加
  • ゲームを再生して確認
  • コインがゆっくり回転すれば成功


(コードの改良1)

  • 下記のコードを書いてチェック
  • チェックが完了したらゲームを再生
  • 「回転速度」が変化していることを確認しましょう。
using UnityEngine;

public class AutoRotate : MonoBehaviour
{
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // コードの追加
        // 「*」は掛け算の記号(ポイント)
        transform.Rotate(new Vector3(0, 0, 90) * Time.deltaTime * 10);
    }
}

(コードの改良2)

  • 下記のコードを書いてチェック
  • チェックが完了したらゲームを再生
  • 「回転の角度」が変化していることを確認しましょう。
using UnityEngine;

public class AutoRotate : MonoBehaviour
{
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // コードの改良
        transform.Rotate(new Vector3(90, 0, 0) * Time.deltaTime * 2);
    }
}