区域与传送器

两块内容:进入区域就把本地玩家传送到目的地,以及区域里显示在线人数。前者是很多”门/电梯/传送门”脚本的基础。

准备

  1. 建一个触发区域:加 Box Collider,勾上 Is Trigger;
  2. 放一个空对象作为目的地(位置+朝向),拖进脚本的 Destination;
  3. 把脚本挂在触发区域上。

脚本

using UnityEngine;
using Cilbox;
using Aeloria.Scripts.AeloriaSdk.Players;

[Cilboxable]
public class ZoneTeleport : MonoBehaviour
{
    public Transform destination;

    void OnTriggerEnter(Collider other)
    {
        // 只处理本地玩家(Player 层是 3,见「场景层级」)
        if (other.gameObject.layer != 3) return;

        AeloriaLocalPlayer local = AeloriaLocalPlayer.Instance;
        if (local == null) return;
        local.Teleport(destination.position, destination.rotation, mode: AeloriaTeleportMode.WorldFeet);
    }
}

OnTriggerEnter 只会在物体进入的瞬间触发一次,所以不会每帧被传送;如果目的地就在触发区里,记得把它放远一点,避免来回传送。

区域里的人数和提示

显示”当前在线”比逐个统计区域更简单,也够大多数大厅用:

using UnityEngine;
using Cilbox;
using TMPro;
using Aeloria.Shims;

[Cilboxable]
public class LobbyCount : MonoBehaviour
{
    public TextMeshProUGUI label;
    float nextUpdate;

    void Update()
    {
        if (Time.time < nextUpdate) return;
        nextUpdate = Time.time + 0.5f;
        label.text = AeloriaPlayersShim.Count.ToString();
    }
}