0

我在抓取大物体时注意到奇怪的相互作用。我正在使用自定义脚本来启用双臂抓取,该脚本主要基于 XRGrabInteractable 脚本。如果我将运动类型设置为瞬时或运动,代码工作正常。但是,当运动类型 = 速度跟踪时,对象会在施加向上或向下的力(不旋转我的手臂)时围绕它自身旋转。

public class DualArmGunManipulation : XRGrabInteractable
{
    public List<XRSimpleInteractable> secondHandPoints = new List<XRSimpleInteractable>();
    private XRBaseInteractor secondInteractor;
    private Quaternion _intitialLocalRotation;
    public bool snapToSecondHand = true;
    public Quaternion initialRotationOffset;
    public enum TwoHandRotationType { None, First, Second };
    public TwoHandRotationType twoHandRotationType;
    private void Start()
    {
        foreach (var item in secondHandPoints)
        {
            item.onSelectEntered.AddListener(OnSecondHandGrab);
            item.onSelectExited.AddListener(OnSecondHandRelease);
        }
    }
    public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
    {
        if (secondInteractor && selectingInteractor)
        {
            //if (secondInteractor.TryGetComponent<XRSocketInteractor>(out XRSocketInteractor xRSocket)) { base.ProcessInteractable(updatePhase); return; }
            //Compute the rotation
            if (snapToSecondHand)
                selectingInteractor.attachTransform.rotation = GetTwoHandRotation();
            else
                selectingInteractor.attachTransform.rotation = GetTwoHandRotation() * initialRotationOffset;
        }
        base.ProcessInteractable(updatePhase);
    }


    public override bool IsSelectableBy(XRBaseInteractor interactor)
    {
        bool _isGrabbed = selectingInteractor && !interactor.Equals(selectingInteractor);

        return base.IsSelectableBy(interactor) && !_isGrabbed;
    }


    private Quaternion GetTwoHandRotation()
    {
        Quaternion targetRotation;
        if (twoHandRotationType == TwoHandRotationType.None)
        {
            targetRotation = Quaternion.LookRotation(secondInteractor.attachTransform.position - selectingInteractor.attachTransform.position);
        }
        else if (twoHandRotationType == TwoHandRotationType.First)
        {
            targetRotation = Quaternion.LookRotation(secondInteractor.attachTransform.position - selectingInteractor.attachTransform.position, selectingInteractor.attachTransform.up);
        }
        else
        {
            targetRotation = Quaternion.LookRotation(secondInteractor.attachTransform.position - selectingInteractor.attachTransform.position, secondInteractor.attachTransform.up);
        }
        return targetRotation;
    }

    public void OnSecondHandGrab(XRBaseInteractor interactor)
    {
        secondInteractor = interactor;
        initialRotationOffset = Quaternion.Inverse(GetTwoHandRotation()) * selectingInteractor.attachTransform.rotation;
    }

    public void OnSecondHandRelease(XRBaseInteractor interactor)
    {
        secondInteractor = null;
        interactor.attachTransform.localRotation = _intitialLocalRotation;
    }

    protected override void OnSelectEntered(XRBaseInteractor interactor)
    {
        base.OnSelectEntered(interactor);
        _intitialLocalRotation = interactor.attachTransform.localRotation;
    }

    protected override void OnSelectExited(XRBaseInteractor interactor)
    {
        base.OnSelectExited(interactor);
        secondInteractor = null;
        interactor.attachTransform.localRotation = _intitialLocalRotation;
    }
}

}

我试图限制它的约束(发生了不同的错误)。我试图施加更大的阻力,问题仍然存在。我正在尝试模拟现实生活中的工作环境,所以..对象物理必须到位。谁能想到解决方法?

**PS:我不知道如何添加视频,所以我附上了脚本和提到的对象。 在此处输入图像描述 在此处 输入图像描述

4

0 回答 0