0
0

Verseでの位置や向きの扱いについて

Last updated at Posted at 2024-06-17

1.はじめに

この記事を読むと、Verseでのプレイヤーやデバイス、建築小道具の位置や角度の取得方法、設定方法がわかります。それがわかると、デバイス・建築小道具にプレイヤーを追跡させたり、プレイヤーを好きな位置にテレポートさせたりすることができます。

2.transform構造体の使い方を知ろう

プレイヤーやデバイス、物体はtransform構造体を持っていて、その中に位置や向きの情報を持っています。なので、transform構造体の中身や使い方を知りましょう。

■これ以降の表記の仕方で1つだけ約束をしておきます。

コードブロック内の[クラス名または型名]という表現は
クラス名または型名のインスタンスや変数を表します。

1)transform構造体の取得方法

①基本的な書き方

[fort_characterまたは、XXX_device、creative_prop].GetTransform()

②サンプルコード

プレイヤーの場合

# ①agentまたは②playerからfort_characterへの変換
# ①agentの場合
FortChar := [agent].GetFortCharacter[]
# ②playerの場合
FortChar := [player].GetFortCharacter[]

# fort_characterのGetTransformメソッドでtransformを取得
Transform := FortChar.GetTransform()

デバイス・建築小道具の場合

# UEFNからデバイスを設定できるようにする
@editable Button : button_device = button_device{}
# UEFNから建築小道具を設定できるようにする
@editable Prop : creative_prop = creative_prop{}

# GetTransformメソッドでtransformを取得
ButtonTransform := Button.GetTransform()
PropTransform := Prop.GetTransform()

2)transform構造体メンバー

①構造体メンバー

構造体のメンバーは以下のとおりです。(公式サイト transform struct

メンバー名 入っている情報
Scale vector3 スケール
Rotation rotation 回転情報
Translation vector3 位置情報

②各メンバー詳細

ⅰ)Scale

取得方法

Scale := FortChar.GetTransform().Scale

データの使い方

vector3なのでTranslationと同様に操作可能です。
Translationの説明を参考にしてください。

ⅱ)Rotation

角度にはYaw,Pitch,Rollがあります。
※Yaw,Pitch,Rollについては以下のページを参考にしてください。
Wikipedia ローリング

取得方法

rotation構造体を取得する方法とrotation構造体から角度を取得する方法です。
※取得できるのは弧度法(radian)ではなく、度数法(degree)の角度になります。

# 回転情報(rotation)を取得 ※FortCharはfort_characterクラスのインスタンスとします
Rotation := FortChar.GetTransform().Rotation

# Yaw,Pitch,Rollの入った配列を取得
Degrees := Rotation.GetYawPitchRollDegrees()

# 配列からYaw,Pitch,Rollを取得
Yaw := Degrees[0]
Pitch := Degrees[1]
Roll := Degrees[2]

データの使い方

# rotation構造体を作る
Rotation := MakeRotationFromYawPitchRollDegrees(Yaw, Pitch, Roll)

# RotationのYawに90度を加える
NewRotation := Rotation.ApplyYaw(90.0)

ⅲ)Translation

XとYは水平方向、Zは高さになります。

取得方法

# 位置(vector3)を取得 ※FortCharはfort_characterクラスのインスタンスとします
Translation := FortChar.GetTransform().Translation

データの使い方

# vector3の作り方
# Xが1.0、Yが2.0、Zが3.0の位置を表すvector3の作成
Translation := vector3{X:=1.0, Y:=2.0, Z:=3.0}

# vector3のオフセット(vector3同士の計算ができます)
# プレイヤーの位置より、Xを1.0、Yを2.0、Zを3.0マイナスした位置を表すvector3の作成
Translation := FortChar.GetTransform().Translation
OffsetTranslation := Translation - vector3{X:=1.0, Y:=2.0, Z:=3.0}

3.さいごに

ここまで読んでくださりありがとうございます。
もし参考になったらいいねなど押してくださると嬉しいです。

この記事は、特にRotationの扱い方で「リンゴのゲーム開発」というサイトを参考にさせていただきました。
この方の記事は参考になるものばかりでいつも参考にさせていただいています。ありがとうございます。
https://ringogames.hatenablog.com/entry/2023/04/02/073146

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