回転し続けるゲームオブジェクトの作り方を紹介します。
Time.deltaTimeを使用することで1秒ごとに指定した角度回転するようにします。
完成動作
ゲームオブジェクトが回転し続けています。
作り方(動画)
作り方(ブログ)
1.シーンに各オブジェクトを配置
1-1.以下のように回転させたいゲームオブジェクト(今回はCube)を配置する。

2.ゲームオブジェクトを回転させるスクリプトを作成
2-1.Rotator.csという名前のスクリプトを作成して、以下のコードを記載する。
using UnityEngine;
public class Rotator : MonoBehaviour
{
[SerializeField]
[Tooltip("x軸の回転角度")]
private float rotateX = 0;
[SerializeField]
[Tooltip("y軸の回転角度")]
private float rotateY = 0;
[SerializeField]
[Tooltip("z軸の回転角度")]
private float rotateZ = 0;
// Update is called once per frame
void Update()
{
// X,Y,Z軸に対してそれぞれ、指定した角度ずつ回転させている。
// deltaTimeをかけることで、フレームごとではなく、1秒ごとに回転するようにしている。
gameObject.transform.Rotate(new Vector3(rotateX, rotateY, rotateZ) * Time.deltaTime);
}
}
3.回転させたいゲームオブジェクトにRotatorスクリプトをアタッチ
3-1.回転させたいゲームオブジェクト(今回はCube)にRotatorスクリプトをアタッチ


3-2.Rotate X(Y,Z)をそれぞれ任意の値にする。

完成
これでゲームオブジェクトが1秒ごとに指定した角度回転し続ける。
コメント