甘酒のアプリ開発

一人でも多くの人を幸せにするアプリを作っていけたらなぁと思います。

【Unity】1分でできる。オブジェクトを回転させる方法

今回は、オブジェクトを回転させる方法 について紹介していきます。

 

簡単に実装することができるので、もしよかったら試してみてください。

f:id:RenRoku6:20210827162601j:plain

 

 

 

オブジェクトを回転させるスクリプト

using UnityEngine;
using System.Collections;

public class RotatesphereY5 : MonoBehaviour
{
void Update()
{
transform.Rotate(new Vector3(0, 5, 0));
}
}

上記のような スクリプトを作ります。

そのままコピペしてもらえればOKです。

 

 

実際にUnityエディターで確認

f:id:RenRoku6:20210827164638p:plain

先ほど作ったRotatesphereY5.csを回転させたいオブジェクトにアタッチします。

 

f:id:RenRoku6:20210827164108p:plain

ゲームスタートボタンを押して確認します。

画像なのでわかりずらいですが、Y軸を軸にして回転しています。

 

X軸、Y軸、Z軸で回転させたい場合のスクリプト

X軸Y軸Z軸で回転させるスクリプトも公開しておきます。

 

using UnityEngine;
using System.Collections;

public class RotatesphereX5 : MonoBehaviour
{
void Update()
{
transform.Rotate(new Vector3(5, 0, 0));
}
}

X軸を軸に回転させたい場合のスクリプト

 

using UnityEngine;
using System.Collections;

public class RotatesphereY5 : MonoBehaviour
{
void Update()
{
transform.Rotate(new Vector3(0, 5, 0));
}
}

Y軸を軸に回転させたい場合のスクリプト

 

using UnityEngine;
using System.Collections;

public class RotatesphereZ5 : MonoBehaviour
{
void Update()
{
transform.Rotate(new Vector3(0, 0, 5));
}
}

Z軸を軸に回転させたい場合のスクリプト

 

 

 

終わり。