【发布时间】:2019-03-06 07:38:00
【问题描述】:
我在测试场景中有基本的对象捕捉,但是当我取消捕捉对象时,捕捉的对象将重新捕捉,除非我很快将其移开。 对于场景设置的上下文,我想在 VR 中实现捕捉对象,因此我在要移动的对象内有一个较小的父对象,以表示该对象将在 VR 中作为父对象的手。此外,我将捕捉脚本(下面的第一个)附加到“手”对象,并在手上使用触发对撞机和它附加到的方形 obj。
有什么办法可以解决这个问题,这样我就不需要快速拉动来解耦对象了吗?
下面是我用来实现这个的脚本
public class snap : MonoBehaviour, collider_helper.collider_help_reciever
{
public Transform snapObj;
public Transform SnapTarget;
bool snapped = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter (Collider other)
{
Debug.Log("enter");
if (snapped == false && other.transform.GetInstanceID() == SnapTarget.GetInstanceID())
{
snapObj.position = SnapTarget.position;
snapObj.rotation = SnapTarget.rotation;
snapObj.parent = SnapTarget;
snapped = true;
}
}
public void OnTriggerExit(Collider other)
{
Debug.Log("exit");
if (snapped == true && other.transform.GetInstanceID() == SnapTarget.GetInstanceID())
{
snapObj.position = transform.position;
snapObj.rotation = transform.rotation;
snapObj.parent = transform;
snapped = false;
StartCoroutine(noSnap);
}
}
}
public class collider_helper : MonoBehaviour {
public GameObject recieving;
collider_help_reciever reciever;
// Use this for initialization
void Start () {
reciever = recieving.GetComponent<collider_help_reciever>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay (Collider other)
{
reciever.OnTriggerEnter(other);
}
public interface collider_help_reciever
{
void OnTriggerEnter(Collider other);
}
}
【问题讨论】: