Example #1
0
    internal void AddToMap(Map map, Vector3 start, Vector3 end, bool fixEnd)
    {
        StartAnchor.transform.position = start;
        EndAnchor.transform.position   = end;

        StartAnchor.transform.rotation = Quaternion.FromToRotation(Vector3.down, end - start);
        EndAnchor.transform.rotation   = Quaternion.FromToRotation(Vector3.up, start - end);

        EndAnchor.gameObject.SetActive(fixEnd);

        int     segCount  = Mathf.CeilToInt((end - start).magnitude / segmentSize);
        Vector3 segOffset = (end - start).normalized * segmentSize;

        Rigidbody prevBody = StartAnchor;

        for (int i = 0; i < segCount; i++)
        {
            Placeable seg = Game.Instance.PrefabsStore.RopeSegment.Create(transform);
            seg.transform.position = start + i * segOffset;
            seg.transform.rotation = StartAnchor.transform.rotation;

            var joint = seg.GetComponent <HingeJoint>();
            joint.connectedBody   = prevBody;
            joint.connectedAnchor = new Vector3(0, i == 0 ? 0 : -segmentSize, 0);
            prevBody = seg.GetComponent <Rigidbody>();

            seg.PlaceToMap(map);
        }

        if (fixEnd)
        {
            EndAnchor.connectedBody   = prevBody;
            EndAnchor.connectedAnchor = new Vector3(0, -segmentSize, 0);
        }
    }