C# Unity-HingeJoint在旋转的自动生成问题中有奇怪的行为吗?

C# Unity-HingeJoint在旋转的自动生成问题中有奇怪的行为吗?,c#,unity3d,unity-editor,C#,Unity3d,Unity Editor,我是一个初学者,我有一个立方体在空中和元素连接他没有旋转像一个节点,我有一对立方体模拟绳子提示关节。我在一个立方体上点击鼠标右键,从一个方向玩家到另一个节点产生绳子,并用玩家连接最后一个元素绳子,这个效果非常好。当玩家没有速度,并阻止刚体元素的旋转时。但有时,若我被跳下并尝试在移动元素中生成绳子,绳子会有奇怪的旋转,有时立方体旋转块。我怎么能用旋转绳工作,或者我不知道,有什么建议吗?谢谢 PS:对不起,我的英语不好 private void CheckMouseActivity() {

我是一个初学者,我有一个立方体在空中和元素连接他没有旋转像一个节点,我有一对立方体模拟绳子提示关节。我在一个立方体上点击鼠标右键,从一个方向玩家到另一个节点产生绳子,并用玩家连接最后一个元素绳子,这个效果非常好。当玩家没有速度,并阻止刚体元素的旋转时。但有时,若我被跳下并尝试在移动元素中生成绳子,绳子会有奇怪的旋转,有时立方体旋转块。我怎么能用旋转绳工作,或者我不知道,有什么建议吗?谢谢

PS:对不起,我的英语不好

private void CheckMouseActivity()
{
        float x = Input.mousePosition.x;
        float y = Input.mousePosition.y;
        float z = Input.mousePosition.z;

        Vector3 point = new Vector3(x, y, z);

        if (Input.GetMouseButton(1) && !m_ropeIsActive)
        {
            m_aiming = true;

            Ray ray = Camera.main.ScreenPointToRay(point);
            GenerateRope(ray);
        }

        if (Input.GetButton("Jump") && m_ropeIsActive)
        {
            for (int i = 0; i < rope.Count; i++)
            {
                DestroyImmediate(rope[i]);
                //rope[i].active = false;
            }

            playerRb.AddForce(new Vector3(6f, 6f), ForceMode.Impulse); 
            m_ropeIsActive = false;
            m_hooked = false;
        }
        
        // check bounds in playing area
}

void GenerateRope(Ray ray)
{
        layerMask = ~layerMask;

        if (Physics.Raycast(ray, out RaycastHit hit, 10f, layerMask))
        {
            if (hit.transform.CompareTag("node") && !m_ropeIsActive)
            {
                activeHook = hit.transform.gameObject;
                m_CanMove = false;
                Vector3 hitPos = hit.transform.position;
                Transform hook = hit.transform.GetChild(0);
                Vector3 hookInverseNormal = hook.up * -1f;

                Rigidbody previosRb = hit.transform.GetChild(0).GetComponent<Rigidbody>();//hook

                Vector3 distanceFromNode = transform.position - hit.point;
                int distance = (int)distanceFromNode.magnitude;

                BoxCollider link = linkPrefub.GetComponent<BoxCollider>();
                int length = (int)((distance - 1f) / sizeCell); 
                float ropeLength = sizeCell * 10f;

                if (length < minLength) 
                {
                    length = minLength;
                }
                //else if (length > ropeLength)
                //{
                //    length = (int)ropeLength * 2;
                //}

                for (int i = 1; i < length - 1; i++) 
                {
                    var t = sizeCell * i; //позиция под новый куб
                    Vector3 dirToPlayer = transform.position - hit.transform.position;
                    Vector3 newPos = dirToPlayer.normalized * t;
                    GameObject toInstantiate = Instantiate(linkPrefub, hit.transform.position + new 
Vector3(newPos.x, -t, default), Quaternion.identity, hit.transform);
                    rope.Add(toInstantiate);
                    HingeJoint joint = toInstantiate.GetComponent<HingeJoint>();

                    if (i == 1)
                    {
                        joint.autoConfigureConnectedAnchor = true;
                    }
                    else
                    {
                        joint.autoConfigureConnectedAnchor = false;
                        joint.connectedAnchor = new Vector3(0f, -0.4f);
                    }

                    joint.enableCollision = false;
                    joint.connectedBody = previosRb;

                    previosRb = toInstantiate.GetComponent<Rigidbody>();
                }

                m_ropeIsActive = true;
                m_hooked = true;
                m_CanMove = true;
            }
        }
        else
        {
            // what to do if not did hit
        }
}

void FixedUpdate()
{
    if (m_ropeIsActive)
    {
            if (m_hooked && activeHook)
            {
                //TODO крутить нод хука чтобы выглядило красиво
            }

            if (rope.Count != 0)
            {
                Rigidbody lastElementRope = rope[rope.Count - 1].GetComponent<Rigidbody>();

                if (lastElementRope)
                {
                    HingeJoint joint = GetComponent<HingeJoint>();
                    //joint.autoConfigureConnectedAnchor = false;
                    //joint.connectedBody = lastElementRope;
                    //joint.anchor = Vector2.zero;
                    //joint.connectedAnchor = new Vector2(0f, -0.4f);
                }
            }
        }
}
GitHub

我不确定,但是

 void GenerateRope(Ray ray)
{
    layerMask = ~layerMask;
    if (Physics.Raycast(ray, out RaycastHit hit, 10f, layerMask))
    {
        Vector3 direction =  transform.position - hit.transform.position;

        if (direction.magnitude <= sizeOfRope)
        {
            if (hit.transform.CompareTag("node") && !m_ropeIsActive)
            {
                activeHook = hit.transform.gameObject;
                m_CanMove = false;
                Transform hook = hit.transform.GetChild(0);
                Vector3 hookInverseNormal = hook.up * -1f;

                float dot = Vector3.Dot(hookInverseNormal, ray.direction.normalized);
                Debug.Log(dot);
                Quaternion rotation = hit.transform.rotation;

                Rigidbody previosRb = hit.transform.GetChild(0).GetComponent<Rigidbody>();//hook

                Vector3 distanceFromNode = transform.position - hit.point;
                int distance = (int)distanceFromNode.magnitude;

                BoxCollider link = linkPrefub.GetComponent<BoxCollider>();//(int)link.size.magnitude / 2;
                int length = (int)((distance - 1f) / sizeCell); //size of cell rope -1f размер игрока
                float ropeLength = sizeCell * sizeOfRope;
                if (length < minLength)
                {
                    length = minLength;
                }
                //else if (length > ropeLength) // и слишком длинных
                //{
                //    length = (int)ropeLength * 2;
                //}
                for (int i = 1; i < length - 1; i++)
                {
                    var t = sizeCell * i; //позиция под новый куб
                    Vector3 dirToPlayer = transform.position - hit.transform.position;//направление до игрока,
                    Vector3 newPos = dirToPlayer.normalized * t;
                    GameObject toInstantiate = Instantiate(linkPrefub, hit.transform.position + new Vector3(newPos.x, -t, default), Quaternion.identity, hit.transform);
                    rope.Add(toInstantiate);
                    HingeJoint joint = toInstantiate.GetComponent<HingeJoint>();
                    joint.anchor = Vector2.up;
                    joint.connectedAnchor = new Vector2(0f, -0.4f);
                    joint.connectedBody = previosRb;
                    previosRb = toInstantiate.GetComponent<Rigidbody>();
                }
                m_ropeIsActive = true;
                m_hooked = true;
            }
        }
        else
        {
            //what to do if not did hit
        }
    }
void GenerateRope(光线)
{
LAYERMAK=~LAYERMAK;
if(物理光线投射(光线,外光线投射命中,10f,层任务))
{
矢量3方向=transform.position-hit.transform.position;
if(方向、长度)//
//{
//长度=(int)绳索长度*2;
//}
对于(int i=1;i
它仍然不能很好地工作,但我的问题解决了。 限制了可以调用绳索的距离,结果是,当自动生成绳索的部分时,提示关节默认为其值​​例如自动锚定。在创建过程中禁用它们,似乎一切都正常工作

HingeJoint joint = toInstantiate.GetComponent<HingeJoint>();
                    joint.anchor = Vector2.up;
                    joint.connectedAnchor = new Vector2(0f, -0.4f);
                    joint.connectedBody = previosRb;
                    previosRb = toInstantiate.GetComponent<Rigidbody>();
HingeJoint=toinstate.GetComponent();
joint.anchor=Vector2.up;
joint.connectedAnchor=新矢量2(0f,-0.4f);
joint.connectedBody=previosRb;
previosRb=toinstate.GetComponent();
HingeJoint joint = toInstantiate.GetComponent<HingeJoint>();
                    joint.anchor = Vector2.up;
                    joint.connectedAnchor = new Vector2(0f, -0.4f);
                    joint.connectedBody = previosRb;
                    previosRb = toInstantiate.GetComponent<Rigidbody>();