0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UdonSharp ワープギミックの作り方

Last updated at Posted at 2025-04-26

UdonSharpを始めよう(ワープギミックの作り方)

前置き

前回の記事の続編です
この記事では、ワールドの基本ギミックである「ワープ」の作り方を解説します

1. 今回作るもの

  • 押すとローカルプレイヤー(自分)がワープするボタン
  • 完成イメージは以下の図です
    image.png

ワープがわかりやすいように、元の床から離れた位置に白い床を作ってみました。

2. 作業の流れ

  • ボタンとなるCubeの作成
  • インタラクトするとワープするコード記述
  • ワープ先となるTransformationの作成
  • 実行して動作確認

3. 作業

■ボタンとなるCubeの作成

  • ヒエラルキーの左上の「+」ボタンから 3D > Cube を選択。
  • 今回は「WarpButton」と命名します

■インタラクトするとワープするコード記述

  • 今回は「Intaract」を使います。これはユーザがボタンにし対してトリガーを引いたときに、記述内容が発動します
public class WarpButton : UdonSharpBehaviour
{
    public override void Interact()
    {

    }

}

ここに、ユーザがワープするプログラムを追記します

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

public class WarpButton : UdonSharpBehaviour
{
    [SerializeField] Transform Destination; // ワープ先のTransform
    
    public override void Interact() // インタラクトしたときに、ここに書かれた内容が発動します
    {
        Networking.LocalPlayer.TeleportTo(Destination.position, Destination.rotation);
        Debug.Log("ユーザがワープしました"); //デバッグログメッセージ
    }

}

解説

  • [SerializeField]は、画像のようにプルダウンまたはドラッグアンドドロップでオブジェクトを選択できる項目を作ります
  • 今回は、この項目に入れるものをTransformに限定し、Destinationという名前で今後扱います
    image.png
  • Networking.LocalPlayerはローカルプレイヤーに対して何かしたいときに使います
  • TeleportTo(向き、回転)で向きと回転を指定してワープさせます。今回はTransformationオブジェクトから.position.rotationで向きと回転を取得しています。もし気になったらDebug.Log(Destination.position)で座標の数値を見てみてください

■ワープ先となるTransformの作成

  • ヒエラルキーの左上の「+」ボタンから CreateEmpty を選択。
  • 「WarpDestination」と命名します。ワープしたい位置に配置します。
  • 先ほど記述したWarpButtonのプログラムを開き、Destinasionの項目に、作成したWarpDestinationオブジェクトを入れます
    image.png

4. 動作確認

  • ゲームモードに切り替えて、ボタンを押す!
    image.png

  • ワープ完了!
    image.png

ワープする向きについて補足します。
青矢印(Z軸)がプレイヤーの正面になります
image.png

5. 応用

  • 本記事では解説しませんが、暗転するアニメーションと組み合わせることで、よりスムーズでゲームチックなワープを演出できます
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?