unity3d+htcvive:手柄震动,扳机角度控制强弱

2023-08-24 14:25:03 浏览数 (2)

代码语言:javascript复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class HandPulse : MonoBehaviour {
    private VRTK_ControllerEvents controllerEvents;
    // Use this for initialization
    void Start () {
        var tempScript = transform.GetComponent<VRTK.VRTK_InteractableObject>();
        tempScript.InteractableObjectUsed  = OnInteractableObjectUsed;
        tempScript.InteractableObjectUnused  = OnInteractableObjectUnused;
        tempScript.InteractableObjectUngrabbed  = OnInteractableObjectUngrabbed;
    }

    // Update is called once per frame
    void Update () {
        if (controllerEvents)
        {
            float power = controllerEvents.GetTriggerAxis();
            VRTK_ControllerHaptics.TriggerHapticPulse(VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject), power * 0.25f, 0.1f, 0.01f);
        }
    }

    void OnInteractableObjectUsed(object sender, InteractableObjectEventArgs e)
    {
        controllerEvents = e.interactingObject.GetComponent<VRTK_ControllerEvents>();
    }

    void OnInteractableObjectUnused(object sender, InteractableObjectEventArgs e)
    {
        controllerEvents = null;
    }

    void OnInteractableObjectUngrabbed(object sender, InteractableObjectEventArgs e)
    {
        controllerEvents = null;
    }
}

把脚本挂在要交互的物体上,手柄按扳机使用物体时,手柄震动,震动的强弱根据扳机按下的角度

0 人点赞