大家好日子。检测到我的手指碰撞到我的物体时,我遇到了一些问题。在我的第一个阶段,我的代码在与我的手指碰撞时销毁对象是:
DetectionCollision.cs
using UnityEngine;
using System.Collections;
using Leap;
using Leap.Unity;
public class GetFlashLight : MonoBehaviour {
// Use this for initialization
void Start () {
}
private HandModel IsHand(Collider other){
if (other.transform.parent && other.transform.parent.parent && other.transform.parent.parent.GetComponent<HandModel> ()) {
return other.transform.parent.parent.parent.GetComponent<HandModel> ();
} else {
return null;
}
}
void OnTriggerEnter(Collider other) {
HandModel hand_model = IsHand (other);
if (hand_model != null) {
this.GetComponent<DisableObject> ().enabled = true;
} else {
this.GetComponent<DisableObject> ().enabled = false;
}
}
}
DisableObject.cs
public GameObject TurnOnFlashLight;
// Use this for initialization
void Start () {
TurnOnFlashLight.gameObject.SetActive (true);
}
问题是当我应用相同的代码但不同的c#脚本(ofcourse) 它没有用。您认为问题是什么?
答案 0 :(得分:0)
您可以尝试以下方法尝试查找其他对手是否有手:
如果您希望对搜索过程进行微调,或者GetComponentInParent限制对您不利,那么第二个版本就放在此处。
using UnityEngine;
internal class HandModel : MonoBehaviour
{
}
internal class MyClass : MonoBehaviour
{
private HandModel TryGetHandVersion1(Collider other)
{
return other.GetComponentInParent<HandModel>();
}
private HandModel TryGetHandVersion2(Collider other)
{
var current = other.transform;
while (current != null)
{
var handModel = current.GetComponent<HandModel>();
if (handModel != null)
return handModel;
current = current.parent;
}
return null;
}
private void OnTriggerEnter(Collider other)
{
var isHand = TryGetHandVersion1(other) != null;
}
}
答案 1 :(得分:0)
0
通过这样做,我得到了我想要的东西。