座位

用脚本控制座位:坐下、转向、起身。适合做”按按钮上车””比赛开始自动入座”这类流程。

准备

场景里已经有一个配好的 Aeloria Seat(座位模型、上下车位置都设置好),把脚本挂在任意对象上,把座位拖进 Seat 字段。

脚本

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

[Cilboxable]
public class SeatControl : MonoBehaviour
{
    public AeloriaSeat seat;

    public void Sit()
    {
        if (seat.IsAvailable) seat.TrySeatLocalPlayer();
    }

    public void Stand()
    {
        if (seat.IsLocalPlayerSeated) seat.EjectLocalPlayer();
    }

    public void Turn(float degrees)
    {
        if (seat.IsLocalPlayerSeated) seat.SetOccupantYaw(degrees);
    }
}

说明

  • IsAvailable:座位空着、并且玩家没有在设置里禁用座位时才为 true;
  • TrySeatLocalPlayer() 返回是否成功,失败通常是座位被占;
  • SetOccupantYaw(degrees) 只影响坐在座位上的人;还有一个 TurnOccupant(delta) 做相对旋转。

显示座位上的人

想知道谁坐在上面,用 TryGetOccupant 拿到玩家再读名字:

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

[Cilboxable]
public class SeatLabel : MonoBehaviour
{
    public AeloriaSeat seat;
    public TextMeshProUGUI label;

    void Update()
    {
        IAeloriaPlayer occupant;
        if (seat.TryGetOccupant(out occupant) && occupant != null)
        {
            label.text = occupant.DisplayName;
        }
        else
        {
            label.text = "";
        }
    }
}