在游戏过程中,弹簧接头的连接锚可以移动吗?



我正在制作一款带有绳索秋千英雄的游戏,其中也有飞行汽车,它们在移动,但我的角色的弹簧关节连接的锚固定在第一次点击点。它不随汽车移动。当我写我的代码,我想如果我参考连接锚车的刚体的位置与更新功能,它应该与车移动,但它没有。我该怎么办?从现在开始谢谢你。

这是我的代码;

void StartGrapple()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, isGrappleable))
{
currentGrapplePosition = player.position;
grapplePoint = hit.rigidbody.position;
joint = player.gameObject.AddComponent<SpringJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = grapplePoint += new Vector3(-4, 0, 0);
float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);
joint.maxDistance = distanceFromPoint * 0.5f;
joint.minDistance = distanceFromPoint * 0f;
joint.spring = 60f;
joint.damper = 40f;
joint.massScale = 4.5f;
lr.positionCount = 2;
webShot.Play();
}
}

仅设置Joint.connectedAnchor是不够的!

相对于所连接刚体的位置.

还要记住

位置以所连接刚体的局部坐标给出,如果没有连接的刚体,则在世界坐标中

您要设置Joint.connectedBody

对该关节连接的另一个刚体的引用。

如果不设置,则该关节将对象连接到世界空间中的一个固定点。

joint.connectedBody = hit.rigidbody;

除此之外

joint.connectedAnchor = grapplePoint += new Vector3(-4, 0, 0);

这基本上等于

grapplePoint = grapplePoint + new Vector3(-4, 0, 0);
joint.connectedAnchor = grapplePoint;

虽然在这种情况下,这可能仍然是有效的,并返回相同的值,但这可能不是您真正想要做的。一般来说,你更希望使用

joint.connectedAnchor = grapplePoint + new Vector3(-4, 0, 0);

一般来说我个人更倾向于简单地使用例如

joint.connectedAnchor = hit.transform.InverseTransformPoint(hit.point);

而不是仅仅是你击中另一个物体的点。

最新更新