[英]Unity3d. CharacterJoint is not correct
I created a scene with two cubes. 我创建了一个包含两个立方体的场景。 I want first cube to fallow rotation of second cube.
我希望第一个多维数据集让第二个多维数据集旋转。 For it to work, I set
joint.targetRotation
and slerpDrive
properties. 为了使其工作,我设置了
joint.targetRotation
和slerpDrive
属性。 And it work fine. 而且工作正常。 But sometimes, I noticed that it start rotating infinitely, like no
slerpDrive
is defined. 但是有时,我注意到它开始无限旋转,就像
slerpDrive
一样。 For example, to reproduce, you can see this script: 例如,要重现,您可以看到以下脚本:
public class TestScript : MonoBehaviour
{
[SerializeField]
Rigidbody _connectedBody;
void Awake()
{
var rigid = GetComponent<Rigidbody>();
var joint = gameObject.AddComponent<ConfigurableJoint>();
joint.autoConfigureConnectedAnchor = false;
joint.connectedBody = _connectedBody;
joint.connectedAnchor = Vector3.zero;
joint.anchor = Vector3.zero;
joint.rotationDriveMode = RotationDriveMode.Slerp;
var drive = new JointDrive()
{
positionSpring = 100f,
maximumForce = float.MaxValue
};
joint.slerpDrive = drive;
// joint.xDrive = drive;
// joint.yDrive = drive;
// joint.zDrive = drive;
joint.targetRotation = Quaternion.Euler(10, 0, 0);
}
void FixedUpdate()
{
var joint = gameObject.GetComponent<ConfigurableJoint>();
joint.axis = Vector3.right;
}
}
A cube with this script is rotating infinitely. 具有此脚本的多维数据集将无限旋转。 Why?
为什么? If you comment this line
joint.axis = Vector3.right;
如果您对此行发表评论,
joint.axis = Vector3.right;
, it stop rotating. ,它停止旋转。 But I do not understand why is it happening.
但是我不明白为什么会这样。
Well, you are calling your joint.axis = Vector3.right;
好吧,您正在调用您的
joint.axis = Vector3.right;
inside of void FixedUpdate()
, Vector3.right
is identical to Vector3(1, 0, 0)
, meaning every update, you are giving a new position to which you are asking it to move to, giving it infinite motion. 在
void FixedUpdate()
内部, Vector3.right
与Vector3(1, 0, 0)
Vector3.right
)相同,这意味着每次更新时,您都在为其赋予一个新的位置,要求它移动到该位置,并使其无限移动。
What you should aim to do, is trigger the movement at the right moment. 您应该做的是在适当的时候触发动作。
I'd advise you to create "Debug bindings" with unity (basically just dedicate some binding to try stuff out) so that you have tactile feedback for your behaviours. 我建议您统一创建“调试绑定”(基本上只是专门进行一些绑定以尝试使用),以便您对自己的行为有触觉反馈。 Giving you easy access to start and stop it.
使您可以轻松启动和停止它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.