10
5

More than 3 years have passed since last update.

UdonSharp日記~ローカルプレイヤーの座標の取得~

Posted at

プレイヤーの座標を得るよ

参考文献

やぎりさんのUdonSharpコード走り書きメモ・・・プレイヤーのトラッキング情報を取得するテスト

やったこと

VRCPlayerApiでユーザーのトラッキングデータにアクセスしてHeadの位置を取得。
それをテキストに入力してワールド上に表示

やり方

まずワールドにCanvasとTextを設置します。
無題.png

そして床のplane(cubeでも何でもよい)にUdonSharpを適用し、次のコードを記載します。

load.cs

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using UnityEngine.UI;

public class Load : UdonSharpBehaviour
{
    public GameObject prefab;//←これは使ってないよ
    public Text debugText;

    void Update()
    {
        var player = Networking.LocalPlayer;

        if (player != null)//Unityの再生ボタンで実行すると変数がnullになるらしいよ
        {
            //playerの位置を取得(位置は頭の位置を使用)
            var headData = player.GetTrackingData(VRCPlayerApi.TrackingDataType.Head);

            debugText.text = string.Format("Head-Pos: {0}\r\n", headData.position.ToString());
        }
    } 
}

そしてこのコードとdebugTextを繋げます。(適切な用語ってあるのかしら?)
無題2.png

結構簡単ですね。

10
5
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
10
5