1

对 VR 来说非常新。我通过抓取从初始位置获取了一个游戏对象。当我抓住头盔并触摸我的身体碰撞器时,它隐藏了头盔。所以接下来我可以选择眼镜并将其应用于我的身体(隐藏游戏对象)。下一步当我戴上不正确的头盔时,第一个头盔应该回到它的初始位置并且应该在场景中看到。同样场景中有很多游戏对象

 private void OnTriggerEnter(Collider other)
{        

    if (other.gameObject.tag == "Helmet")
    {

       
        HideGameObject();
      
       
    }

    if (other.gameObject.tag == "Glasses")
    {

        HideGameObject();           
       
    }
    if (other.gameObject.tag == "EarMuff")
    {
        HideGameObject();
       
    }
   
    

    if (other.gameObject.tag == "IncorrectHelmet")
    {

      
        HideGameObject();
       

    }

    if (other.gameObject.tag == "IncorrectGlasses")
    {
        HideGameObject();
        

    }


    if (other.gameObject.tag == "IncorrectEarMuff")
    {
        HideGameObject();
        sendPickValues.Invoke(2, 0);

    }
    


 }

//另一个设置游戏对象位置的脚本

public class BackToPosition : MonoBehaviour
{

private Vector3 initialPosition;
private Quaternion initialRotation;

GameObject prevObject;
GameObject currObject;



// Start is called before the first frame update
void Start()
{
    initialPosition = transform.position;
    initialRotation = transform.rotation;
}

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

public void BackToInitialPosition()
{
    Debug.Log("Entered");
    transform.position = initialPosition;
    transform.rotation = initialRotation;
                               
    
}
}

我不是想将先前抓取的对象设置为初始位置。我可能会先选择错误的头盔,然后选择许多其他匹配的游戏对象,然后再更换为正确的头盔。那时第一个头盔应该回到初始位置。

4

1 回答 1

1

这是我在 SteamVR 中用来抓取和释放船舵手柄的脚本,但它也应该对你有用:

[RequireComponent(typeof(Interactable))]
public class HandAttacher : MonoBehaviour
{
    public UnityEvent OnGrab;
    public UnityEvent OnRelease;
    public UnityEvent OnHandEnter;
    public UnityEvent OnHandLeave;

    private Interactable interactable;

    void Awake()
    {
        interactable = GetComponent<Interactable>();
    }

    /// this magic method is called by hand while hovering
    protected virtual void HandHoverUpdate(Hand hand)
    {
        GrabTypes startingGrabType = hand.GetGrabStarting();

        if (interactable.attachedToHand == null && startingGrabType != GrabTypes.None)
        {
            hand.AttachObject(gameObject, startingGrabType, Hand.AttachmentFlags.DetachFromOtherHand | Hand.AttachmentFlags.ParentToHand);
            OnGrab?.Invoke();
        }
    }

    protected virtual void OnHandHoverBegin(Hand hand)
    {
        OnHandEnter?.Invoke();
    }

    protected virtual void OnHandHoverEnd(Hand hand)
    {
        OnHandLeave?.Invoke();
    }

    protected virtual void HandAttachedUpdate(Hand hand)
    {
        if (hand.IsGrabEnding(gameObject))
        {
            hand.DetachObject(gameObject);
            OnRelease?.Invoke();
        }
    }
}

基本上它会创建 Unity 事件,您可以在编辑器的检查器窗口或代码中添加监听器。

因此,在您的用例中,我将添加一个侦听器OnRelease,并将 GameObject 的位置和旋转重置为之前的任何值。

于 2020-08-24T12:10:40.153 回答