Exemplo n.º 1
0
    //create rope
    void Create()
    {
        //if rope width is 0, that means that user hadn't set rope width, in that case we make width same as chainObject's renderer size
        if (ropeWidth <= 0.0f)
        {
            ropeWidth = chainObject.renderer.bounds.size.x;
        }

        //if connectToA is set to true and pointA doesn't have DistanceJoint2D component yet, add it
        if (connectToA)
        {
            var jointA = pointA.GetComponent <DistanceJoint2D>();
            if (!jointA || (jointA && jointA.connectedBody))
            {
                pointA.gameObject.AddComponent <DistanceJoint2D>();
                pointA.rigidbody2D.isKinematic = true;
            }
        }

        //if connectToB is set to true and pointB doesn't have DistanceJoint2D component yet, add it
        if (connectToB)
        {
            var jointB = pointB.GetComponent <DistanceJoint2D>();
            if (!jointB || (jointB && jointB.connectedBody))
            {
                pointB.gameObject.AddComponent <DistanceJoint2D>();
                //pointB.rigidbody2D.isKinematic = true;
            }
        }

        //create "Chains Holder" object, used to make chains children of that object
        chainsHolder = new GameObject("Chains Holder");
        chainsHolder.AddComponent("CadenaCompletaScriptMastrom");


        //uncomment this if you want to create rope instantly
//		rope.CreateRope (chainsHolder, chainObject, pointA, pointB, lockFirstChain, lockLastChain, connectToA, connectToB, hideEndChains, useLineRenderer, ropeMat, ropeWidth);

        //this function uses delay between chain creation
        StartCoroutine(rope.CreateRopeWithDelay(chainsHolder, chainObject, pointA, pointB, lockFirstChain, lockLastChain, connectToA, connectToB, hideEndChains, useLineRenderer, ropeMat, ropeWidth, delay));
    }
Exemplo n.º 2
0
    //create rope
    void Create()
    {
        if (!chainObject.GetComponent <Collider2D>())
        {
            Debug.LogWarning("Chain Object Doesn't Have Collider2D Attached");
            return;
        }

        if (!chainObject.GetComponent <HingeJoint2D>())
        {
            Debug.LogWarning("Chain Object Doesn't Have HingeJoint2D Attached");
            return;
        }

        //if rope width is 0, that means that user hadn't set rope width, in that case we make width same as chainObject's renderer size
        if (ropeWidth <= 0.0f)
        {
            ropeWidth = chainObject.GetComponent <Renderer>().bounds.size.x;
        }

        //if pointA has 3D collider attached, remove it
        var colA = pointA.GetComponent <Collider>();

        if (colA)
        {
            DestroyImmediate(colA);
        }

        //if connectToA is set to true and pointA doesn't have DistanceJoint2D component yet, add it
        if (connectToA)
        {
            var jointA = pointA.GetComponent <DistanceJoint2D>();
            if (!jointA || (jointA && jointA.connectedBody))
            {
                pointA.gameObject.AddComponent <DistanceJoint2D>();
                pointA.GetComponent <Rigidbody2D>().isKinematic = true;
            }
        }

        //if pointB has 3D collider attached, remove it
        var colB = pointB.GetComponent <Collider>();

        if (colB)
        {
            DestroyImmediate(colB);
        }

        //if connectToB is set to true and pointB doesn't have DistanceJoint2D component yet, add it
        if (connectToB)
        {
            var jointB = pointB.GetComponent <DistanceJoint2D>();
            if (!jointB || (jointB && jointB.connectedBody))
            {
                pointB.gameObject.AddComponent <DistanceJoint2D>();
                pointB.GetComponent <Rigidbody2D>().isKinematic = true;
            }
        }

        //calculate how many chains is needed from pointA to pointB
        var chainCount = (int)(Vector3.Distance(pointA.position, pointB.position) / (chainObject.GetComponent <Renderer>().bounds.extents.x * 1.9f));

        if (chainCount < 2)
        {
            Debug.LogWarning("Distance from " + pointA.name + " (PointA) to " + pointB.name + " (PointB) is very small, increase distance");
            return;
        }

        //create "Chains Holder" object, used to make chains children of that object
        chainsHolder = new GameObject("Chains Holder");

        //uncomment this if you want to create rope instantly
//		rope.CreateRope (chainsHolder, chainObject, pointA, pointB, lockFirstChain, lockLastChain, connectToA, connectToB, hideEndChains, useLineRenderer, ropeMat, ropeWidth, ropeLength);

        //this function uses delay between chain creation
        StartCoroutine(rope.CreateRopeWithDelay(chainsHolder, chainObject, pointA, pointB, lockFirstChain, lockLastChain, connectToA, connectToB, hideEndChains, useLineRenderer, ropeMat, ropeWidth, ropeLength, delay));
    }