如果你不想让它只是一个孩子,我会自然而然地看到 3 个选项:
限制一个对象的移动依赖于另一个对象。这有点类似于 Parenting,但通过物理而不是 Transform 层次结构实现。使用它们的最佳场景是当您想要轻松地将对象分开时,或者在没有父级的情况下连接两个对象的移动。
所以每当你抓住物体时,做一些类似的事情
var fixedJoint = grabbedObject.AddComponent<FixedJoint>();
fixedJoint.connectedBody = controllerObject.GetComponent<RigidBody>();
//disable autoconfigure anchor
fixedJoint.autoConfigureConnectedAnchor = false;
// And set offsets depending on the current relative positions
// Get the position of the object in the controllers local coordinates
fixedJoint.connectedAnchor = controllerObject.transform.InverseTransformPoint(grabbedObject.transform.position);
// And get the controllers position relative to the object
fixedJoint.anchor = gtabbedObject.transform.InverseTransformPoint(controller.transform.posotion);
无论你在哪里释放对象,都要移除那个关节,例如
Destroy(grabbedObject.GetComponent<FixedJoint>());
2。计算变换
获取相对位置偏移量,您也可以使用一种伪造的父子关系,只需使用控制器本地坐标系按组件方式添加偏移量。
在可抓取对象上有类似的组件
public class CopyTransforms : MonoBehaviour
{
private Transform target;
private Transform startOffset;
private RigidBody rb;
private void Awake()
{
rb = GetComponent<RigidBody>();
}
// Call this In the moment you grab the object
public void Grab(Transform grabber)
{
target = grabber;
offset = transform.position - target.position;
}
// Call this when releasing
public void Release()
{
target = null;
}
// If dealing with rigidbodies you should use fixedupdate
private void FixedUpdate()
{
if(!target) return;
// Apply rotation
rb.MoveRotation(target.rotation);
var targetPosition = target.position + target.right * offset.x + target.up * offset.y + target.forward * target.z;
rb.MovePosition(targetPosition);
}
}
3。使用假养育方式
这个想法是在控制器下有一个空对象。
在您抓取的那一刻,您将此空对象的位置设置为您抓取的对象所在的位置 -> 将来,每当移动控制器时,此空对象将是您抓取的对象应该在的位置。所以你只需要应用它的转换并完成
var emptyObject = new GameObject("empty");
emptyObject.transform.SetParent(controllerObject.trasform, false);
emptyObject.transform.rotation = grabbedObject.transform.rotation;
emptyObject.transform.position = grabbedObject.transform.position;
现在您必须简单地复制其转换,例如在
void Update()
{
grabbedObject.transform.rotation = emptyObject.transform.rotation;
grabbedObject.transform.position = emptyObject.transform.position;
}
或像以前一样在FixedUpdate 中。
注意:在智能手机上打字,所以没有保修,但我希望这个想法能清楚。