Unity VR 自行实现的抓取功能包含激活物体上的重力

Posted

技术标签:

【中文标题】Unity VR 自行实现的抓取功能包含激活物体上的重力【英文标题】:Unity VR self implemented grab function contains activates gravity on Objects 【发布时间】:2021-10-07 21:57:15 【问题描述】:

所以我实现了自己的抓取功能,但如果我突然抓取一个对象,重力会将其向下推。尽管Rigidbody 中没有检查重力。这是我将手和抓取的对象融合在一起的代码:

if (_isGrabbing || _heldObject)
    return;

Collider[] grabbableColliders = Physics.OverlapSphere(palm.position, reachDistance, grabbableLayer);
if (grabbableColliders.Length < 1)

    Debug.Log("No Colliders found");
            return;


var objectToGrab = grabbableColliders[0].transform.gameObject;

var objectBody = objectToGrab.GetComponent<Rigidbody>();

if (objectBody != null)

    _heldObject = objectBody.gameObject;

else

    objectBody = objectToGrab.GetComponentInParent<Rigidbody>();
    if (objectBody != null)
    
        _heldObject = objectBody.gameObject;
    
    else
    
        Debug.Log("ObjectBody null");
        return;
    


_isGrabbing = true;

// Create a grab point
_grabPoint = new GameObject().transform;
_grabPoint.position = grabbableColliders[0].ClosestPoint(palm.position);
_grabPoint.parent = _heldObject.transform;

// Move hand to grab point
hand.transform.position = _grabPoint.transform.position;
hand.transform.rotation = _grabPoint.rotation;

objectBody.collisionDetectionMode = CollisionDetectionMode.Continuous;
objectBody.interpolation = RigidbodyInterpolation.Interpolate;

// Attach joints
_joint1 = gameObject.AddComponent<FixedJoint>();
_joint1.connectedBody = objectBody;
_joint1.breakForce = float.PositiveInfinity;
_joint1.breakTorque = float.PositiveInfinity;

_joint1.connectedMassScale = 1;
_joint1.massScale = 1;
_joint1.enableCollision = false;
_joint1.enablePreprocessing = false;

_joint2 = _heldObject.AddComponent<FixedJoint>();
_joint2.connectedBody = _handRigidbody;
_joint2.breakForce = float.PositiveInfinity;
_joint2.breakTorque = float.PositiveInfinity;

_joint2.connectedMassScale = 1;
_joint2.massScale = 1;
_joint2.enableCollision = false;
_joint2.enablePreprocessing = false;
objectBody.useGravity = false;

希望有人能帮帮我。

我正在使用 OpenXR 工具包并使用 Unity 2020.3.4f1。

【问题讨论】:

可能是您的“手”对撞机正在碰撞(并向下推)您抓取的对象? 【参考方案1】:

解决方案很简单。通过将 Hands Rigidbody 检查为 is Kinematic 为真。因此,该对象只能通过它的 Transform 来操作。

【讨论】:

以上是关于Unity VR 自行实现的抓取功能包含激活物体上的重力的主要内容,如果未能解决你的问题,请参考以下文章

Unity5.6之前版本VRTK插件基础交互

转载Unity3D VR 教程:3.VR中的交互

unity的VR场景中怎么实现点击物体播放音乐

Complete Paint丨一款Unity可实现《UI物体VR绘画》的插件

unity HTC Vive 通过手柄控制3D物体移动、旋转

HTC Vive怎么用手柄去旋转物体