脚本示例

手电筒

使用 BasisBeforeRenderShim 和控制器输入,将手电筒无延迟地固定在本地玩家的手上。

HandFlashlight.cs (Cilbox, world)
using Basis.Scripts.BasisSdk.Players;
using Basis.Shims;
using Cilbox;
using UnityEngine;

[Cilboxable]
public class HandFlashlight : MonoBehaviour
{
    public Light beam;

    void Start()
    {
        GetComponent<BasisBeforeRenderShim>();
    }

    void Update()
    {
        if (BasisControllerInputShim.GetButtonDown(BasisControllerHand.Right, BasisControllerButton.Secondary))
            beam.enabled = !beam.enabled;
    }

    void OnBeforeRender()
    {
        IBasisPlayer me = BasisPlayersShim.Local;
        if (me == null) return;
        transform.SetPositionAndRotation(me.GetBonePosition(HumanBodyBones.RightHand), me.GetBoneRotation(HumanBodyBones.RightHand));
    }
}
  • 如果在 Update 中写入位置,在 VR 中会比手慢一帧,因为 IK 完成得更晚。OnBeforeRender 在 IK 之后运行。
  • 这盏灯是本地的:每位玩家都有自己的手电筒,其他人看不到。
Edit on GitHub

Last updated on