LoginSignup
0
0

HeliScriptでアイテムを回転させる【コピペで使えるサンプルコード付き】

Last updated at Posted at 2024-04-29

本記事は、HeliScriptでリッチなアスレチックワールドを作ろう!【コピペで使えるサンプルコード付き】の記事の一部です

VR法人HIKKYのorganization下のQiita/Zenn両方に投稿しております。

はじめに

アイテムを移動させるコンポーネントは、HeliScriptでアイテムをまっすぐ動かす【コピペで使えるサンプルコード付き】 や、HeliScriptでアイテムを反復して動かす【コピペで使えるサンプルコード付き】 で作成したので、今度は、アイテムを回転させるコンポーネントを作っていきます。

Vket Cloudでは、アイテムの回転はUnityなどと同じく、クオータニオン(Quaternion)で表現されています。
HeliScriptでは、Quaternionクラスという組み込みクラスとして用意されており、Itemクラスには、SetQuaternion(Quaternion Rotate) が用意されているため、こちらを使用して時計回りに一定時間で回転させる細長い足場を実装してみます。

また、プロパティとしては、Vector3のオイラー角で1秒間に何度回転するかと、その方向を設定できるようにします。

2024-04-21 22-10-11.gif

Unity設定画面
image.png

コード全文

Utility.hs
```C#:ItemRepetitionMove.hs
component ItemRotate
{
    Utility m_UT; //Utilityクラスを宣言する
	Item m_item;
	
	Vector3 m_rotationEuler; //回転スピード
	float m_offset; //回転の開始地点

	Quaternion m_itemInitialQua;
	float m_timeCounter;

	public ItemRotate()
	{
        m_UT = new Utility(); //Utilityクラスを初期化する
		m_item = hsItemGetSelf();

		m_rotationEuler = m_UT.StrToVector3(m_item.GetProperty("rotationEuler(Vector3)"));

        //アイテムが配置された初期回転値を取得しておく
		m_itemInitialQua = m_item.GetQuaternion();
	}

    //毎フレーム呼びだされる
	public void Update()
	{
        RotateItem();
	}

    //アイテムを回転させる
    void RotateItem()
    {
        //このフレームでのdeltaTimeを取得しておく
        float deltaTime = hsSystemGetDeltaTime();

        //Vector3型のそれぞれの要素にdeltaTimeを掛けた、rotationDirectionを格納する
        Vector3 deltaRotationEuler = makeVector3(deltaTime * m_rotationEuler.x, deltaTime * m_rotationEuler.y, deltaTime * m_rotationEuler.z);

        //Quaternionに変換する
        Quaternion deltaRotation = makeQuaternionEuler(deltaRotationEuler.x, deltaRotationEuler.y, deltaRotationEuler.z);

        //今回のフレームで回転する値を求める
        Quaternion currentRotationQua = makeQuaternionMul(m_item.GetQuaternion(), deltaRotation);

        //回転した値を設定する
		m_item.SetQuaternion(currentRotationQua);
    }
}

コード解説

今回もstring型のpropertyを、Vector3に変換するため、前回の記事で作成した、Utilityクラスの、StrToVector3(string str)を使用しています。

アイテムを回転させる

    //アイテムを回転させる
    void RotateItem()
    {
        //このフレームでのdeltaTimeを取得しておく
        float deltaTime = hsSystemGetDeltaTime();

        //Vector3型のそれぞれの要素にdeltaTimeを掛けた、rotationDirectionを格納する
        Vector3 deltaRotationEuler = makeVector3(deltaTime * m_rotationEuler.x, deltaTime * m_rotationEuler.y, deltaTime * m_rotationEuler.z);

        //Quaternionに変換する
        Quaternion deltaRotation = makeQuaternionEuler(deltaRotationEuler.x, deltaRotationEuler.y, deltaRotationEuler.z);

        //今回のフレームで回転する値を求める
        Quaternion currentRotationQua = makeQuaternionMul(m_item.GetQuaternion(), deltaRotation);

        //回転した値を設定する
		m_item.SetQuaternion(currentRotationQua);
    }

回転を表現する方法として、オイラー角(Euler)と、クオータニオン(Quaternion)があります。オイラー角はUnity等でよく見るVector3での表現方法です。

image.png

Propertyとして設定するには、クオータニオンより、オイラー角の方がやりやすいので、makeQuaternionEuler(float, float, float)を使用して、オイラー角をクオータニオンに変換します。

HeliScriptで二つのクオータニオンを合成するには、makeQuaternionMul(Quaternion, Quaternion)を使用します。

前 : ワールドを一通り遊べるようにする
次 : HeliScriptで振り子を動かす【コピペで使えるサンプルコード付き】】

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0