77 lines
1.9 KiB
C#
77 lines
1.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.XR.PXR;
|
|
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
|
|
public class HandTakeObj : MonoBehaviour
|
|
{
|
|
bool isEnterObj;
|
|
Transform goodTran;
|
|
public PXR_Input.Controller hand;
|
|
XRNode xrNode;
|
|
public Transform goodParent;
|
|
Good good;
|
|
|
|
void Start()
|
|
{
|
|
switch (hand)
|
|
{
|
|
case PXR_Input.Controller.LeftController:
|
|
xrNode = XRNode.LeftHand;
|
|
break;
|
|
case PXR_Input.Controller.RightController:
|
|
xrNode = XRNode.RightHand;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (isEnterObj)
|
|
{
|
|
if (InputDevices.GetDeviceAtXRNode(xrNode).TryGetFeatureValue(CommonUsages.trigger, out float triggerValue))
|
|
{
|
|
if (triggerValue > 0.6f)
|
|
{
|
|
goodTran.SetParent(transform);
|
|
good.IsJiaohu = true;
|
|
}
|
|
else
|
|
{
|
|
goodTran.SetParent(goodParent);
|
|
good.IsJiaohu = false;
|
|
good.sendGoodPos();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (goodTran == null && other.gameObject.layer == 6)
|
|
{
|
|
// 通过不断查找父物体,直到没有父物体为止,获取最上层父物体
|
|
goodTran = other.transform;
|
|
while (goodTran.parent != goodParent)
|
|
{
|
|
goodTran = goodTran.parent;
|
|
}
|
|
good = goodTran.GetComponent<Good>();
|
|
isEnterObj = true;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (goodTran != null && other.gameObject.layer == 6)
|
|
{
|
|
goodTran = null;
|
|
isEnterObj = false;
|
|
}
|
|
}
|
|
}
|