简体   繁体   English

Unity3d。 CharacterJoint不正确

[英]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.targetRotationslerpDrive属性。 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.rightVector3(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.

相关问题 Unity3d。 RenderTexture 与视频播放器 - Unity3d. RenderTexture with Video Player Unity3D。 使用自定义检查器实例化和显示枚举对象 - Unity3D. Instantiating and displaying enum objects using custom inspector Unity3D。 如何在多人射击游戏中正确处理弹丸? - Unity3D. How to properly handle projectile in a multiplayer shooter? 在Unity3D中使用VST.net调用VST插件。 可能吗? - Calling a VST plugin with VST.net in Unity3D. Is it possible? Unity3D。 撞机对模型无反应 - Unity3D. Colliders don't react to model Unity3d。 Handles.ScaleValueHandle更改大小 - Unity3d. Handles.ScaleValueHandle changes size Unity3D。 试图在没有权限的情况下为对象发送命令 - Unity3D. Trying to send command for object without authority 对体素进行动画处理,就好像它们是unity3d中的2d精灵一样。 有没有比这更好的方法了? - Animating voxels as if they were 2d sprites in unity3d. Is there a better way than this? Unity3D。 访问相机的手电筒/闪光灯问题-手电筒可以工作,但在流式传输网络摄像头时无法访问 - Unity3D. Accessing camera torch/flash issues - Torch works but can't access while streaming webcamtexture 我无法在 Unity3D 中构建 android 应用程序。 错误 Gradle 初始化失败 - I am not able to build android app in Unity3D. Error Gradle initialization failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM