Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Rope around collider
#6
(31-01-2023, 04:20 PM)alex798 Wrote: void Update()
{
if (isGrabbed)
cursor.ChangeLength(rope.CalculateLength());
}

change Length like this- is this right?- because when i grab it becomes too long as it was heavily compressed (with 0 compress in distance). Another problem is gravity- should i stretch only if Calculate is much more than starting, or what?

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.

(31-01-2023, 04:20 PM)alex798 Wrote: Whats the meaning of restLenght?- I thought that it was starting lenght, but it changes in runtime...

"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.

(31-01-2023, 04:20 PM)alex798 Wrote: Adjusted to change only on armstretch, but now have a problem, that if our arms becomes closer the lenght don't shorten- I'd use armsTransforms instead of ropeCalculate, but in that case will be problem with stretching around object- armsPositions.magnitude can be small- any suggestions?

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);
        }
    }
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