(Unity6_BRP)オブジェクトをランダムにワープさせる

(スクリプトの作成)

  • 新規にC#スクリプトの作成
  • 名前を「RandomWarp」に変更
  • 下記のコードを書いてチェック
using UnityEngine;

public class RandomWarp : MonoBehaviour
{
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        // 「ゲーム開始後2秒後」から開始
        // 「1.5秒間隔」でワープを実行
        InvokeRepeating("Warp", 2f, 1.5f);
    }

    void Warp()
    {
        transform.position = new Vector3(Random.Range(-0.2f, 0.2f), Random.Range(0.05f, 0.2f), Random.Range(-0.2f, 0.2f));
    }
}

(実行確認)

  • スクリプトをワープさせたいオブジェクトに追加
  • ゲームを再生
  • オブジェクトが一定の範囲内を一定時間ごとにランダムにワープすれば成功です。