unity3D VR開發 手柄射線的事件回調增加

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 定義一個射線類用於回調 記得增加一個射線控制模塊
/// </summary>
public class LineEvent : MonoBehaviour
{

    SteamVR_LaserPointer laserPoniter; //定義一個射線組件的對象
                                       // Use this for initialization
    void Start()
    {
        laserPoniter = GetComponent<SteamVR_LaserPointer>(); //將這個對象獲得射線組件
        laserPoniter.PointerIn += OnPointerIn;//增加回調事件
        laserPoniter.PointerOut += OnPointerInOut;//增加回調事件
    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnPointerIn(object sender, PointerEventArgs e)//定義函數事件(當射線碰撞到物體時)
    {
       print(e.target.gameObject.name) ;//獲取射線碰觸的物體對象的名字
    }
    void OnPointerInOut(object sender, PointerEventArgs e)//定義函數事件(當射線離開物體時)
    {
        print(e.target.gameObject.name);//當射線離開時物體對象的名字
    }
}
發佈了29 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章