【发布时间】: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。
【问题讨论】:
-
可能是您的“手”对撞机正在碰撞(并向下推)您抓取的对象?
标签: c# unity3d virtual-reality