Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Rope around collider
#7
(31-01-2023, 09:38 PM)josemendez Wrote: Nope, doing this will result in the rope increasing its length non-stop. The reason is simple: any small force applied to the rope that stretches it even if it's a tiny bit, will cause the rope to increase its rest length to "absorb" the stretching.

You should only ChangeLength() when the calculated length exceeds the restLength by some predefined amount > 0.


"restLength" is the length that the rope at rest, as the name implies. It changes as a result of calling ChangeLength() because...well, it changes the rope's length Sonrisa.


Try to reduce your rope's length by a small amount every frame, to constantly "reel" it in. Only increase its length if it gets too stretched. There's an included utility script that does this, pasting it here for your convenience:

Code:
[RequireComponent(typeof(ObiRopeCursor))]
    public class ObiRopeReel : MonoBehaviour
    {
        private ObiRopeCursor cursor;
        private ObiRope rope;

        [Header("Roll out/in thresholds")]
        public float outThreshold = 0.8f;
        public float inThreshold = 0.4f;

        [Header("Roll out/in speeds")]
        public float outSpeed = 0.05f;
        public float inSpeed = 0.15f;

        public void Awake()
        {
            cursor = GetComponent<ObiRopeCursor>();
            rope = GetComponent<ObiRope>();
        }

        public void OnValidate()
        {
            // Make sure the range thresholds don't cross:
            outThreshold = Mathf.Max(inThreshold, outThreshold);
        }

        // Update is called once per frame
        void Update()
        {
            // get current and rest lengths:
            float length = rope.CalculateLength();
            float restLength = rope.restLength;

            // calculate difference between current length and rest length:
            float diff = Mathf.Max(0, length - restLength);

            // if the rope has been stretched beyond the reel out threshold, increase its rest length:
            if (diff > outThreshold)
                restLength += diff * outSpeed;

            // if the rope is not stretched past the reel in threshold, decrease its rest length:
            if (diff < inThreshold)
                restLength -= diff * inSpeed;

            // set the new rest length:
            cursor.ChangeLength(restLength);
        }
    }

Thanks a lot, but now have another problem- created particles create weight that affected by gravity create more stretch- in some cases it ruin rope... And this new gravity stretch prevent rope to rollBack restLenght. Tryed to create 0 weight particle and clone it, but in this case particles don't stretch parallel (because of weight difference)...
Reply


Messages In This Thread
Rope around collider - by alex798 - 30-01-2023, 05:30 PM
RE: Rope around collider - by josemendez - 31-01-2023, 11:47 AM
RE: Rope around collider - by alex798 - 31-01-2023, 02:55 PM
RE: Rope around collider - by josemendez - 31-01-2023, 03:19 PM
RE: Rope around collider - by alex798 - 31-01-2023, 04:20 PM
RE: Rope around collider - by josemendez - 31-01-2023, 09:38 PM
RE: Rope around collider - by alex798 - 01-02-2023, 10:51 AM
RE: Rope around collider - by josemendez - 01-02-2023, 11:13 AM
RE: Rope around collider - by alex798 - 01-02-2023, 11:45 AM
RE: Rope around collider - by josemendez - 01-02-2023, 11:51 AM
RE: Rope around collider - by alex798 - 02-02-2023, 10:38 AM
RE: Rope around collider - by josemendez - 02-02-2023, 11:03 AM
RE: Rope around collider - by alex798 - 02-02-2023, 11:26 AM
RE: Rope around collider - by josemendez - 02-02-2023, 01:42 PM
RE: Rope around collider - by alex798 - 02-02-2023, 02:28 PM