在Unity中使用SteamVR手动抓取结束后,如何将游戏对象的位置设置为初始位置?

问题描述

VR相当新。我通过抓握从初始位置抓起一个游戏对象。当我抓紧头盔并触摸我的身体碰撞器时,它会将头盔隐藏起来。所以接下来我可以摘下眼镜并将其应用于我的身体(隐藏游戏对象) )。接下来,当我放置不正确的头盔时,第一个头盔应回到其初始位置并应在场景中看到。在场景中类似地有很多GameObjects

 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);

    }
    


 }

//另一个用于设置GameObjects位置的脚本

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;
                               
    
}
}

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

解决方法

这是我在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的位置和旋转重置为之前的位置。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...