当我把一个物体放在绳子上时,我的绳子会无限伸展

my rope stretches infinitely when i put an object on it

我有一条由纯代码在脚本中生成的绳索,用于统一钓鱼线,这里的问题是当我将钓鱼浮标放在绳索尖端时,绳索无限延伸,我想要浮标悬挂,我试着改变绳子的长度和段的大小,但它一直在拉伸,我也知道铰链关节 2d 从统一中还有另一种方法,但我认为它不够现实,无法制作钓鱼线

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class hiloCanya : MonoBehaviour
{
    private LineRenderer lineRenderer;
    private List<HiloSegmento> hiloSegmentos = new List<HiloSegmento>();
    private float hiloSegLen = 0.25f;
    private int segmentoLength = 35;
    private float lineWidth = 0.05f;

    [SerializeField] private Transform startPoint;
    [SerializeField] private Transform endPoint;

    // Use this for initialization
    void Start()
    {
        this.lineRenderer = this.GetComponent<LineRenderer>();
        Vector3 hiloStartPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        for (int i = 0; i < segmentoLength; i++)
        {
            this.hiloSegmentos.Add(new HiloSegmento(hiloStartPoint));
            hiloStartPoint.y -= hiloSegLen;
        }
    }

    // Update is called once per frame
    void Update()
    {
        this.DrawRope();
    }

    private void FixedUpdate()
    {
        this.Simulate();
    }

    private void Simulate()
    {
        // SIMULACION
        Vector2 fuerzaGravedad = new Vector2(0f, -1.5f);

        for (int i = 1; i < this.segmentoLength; i++)
        {
            HiloSegmento primerSegmento = this.hiloSegmentos[i];
            Vector2 velocidad = primerSegmento.posNow - primerSegmento.posOld;

            primerSegmento.posOld = primerSegmento.posNow;
            primerSegmento.posNow += velocidad;
            primerSegmento.posNow += fuerzaGravedad * Time.fixedDeltaTime;

            this.hiloSegmentos[i] = primerSegmento;
        }

        //CONSTRAINTS
        for (int i = 0; i < 50; i++)
        {
            this.ApplyConstraint();
        }
    }

    private void ApplyConstraint()
    {
        //Constrant to Mouse

        /* HiloSegmento primerSegmento = this.hiloSegmentos[0];
        primerSegmento.posNow = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        this.hiloSegmentos[0] = primerSegmento; */

        //objeto para el principio del hilo

        HiloSegmento primerSegmento = this.hiloSegmentos[0];
        primerSegmento.posNow = this.startPoint.position;
        this.hiloSegmentos[0] = primerSegmento;

        //objeto para el final del hilo
        HiloSegmento finalSegmento = this.hiloSegmentos[this.segmentoLength - 1];
        finalSegmento.posNow = this.endPoint.position;
        this.hiloSegmentos[this.segmentoLength - 1] = finalSegmento;

        for (int i = 0; i < this.segmentoLength - 1; i++)
        {
            HiloSegmento primerSeg = this.hiloSegmentos[i];
            HiloSegmento segundoSeg = this.hiloSegmentos[i + 1];
            float dist = (primerSeg.posNow - segundoSeg.posNow).magnitude;
            float error = Mathf.Abs(dist - this.hiloSegLen);
            Vector2 changeDir = Vector2.zero;

            if (dist > hiloSegLen)
            {
                changeDir = (primerSeg.posNow - segundoSeg.posNow).normalized;
            }
            else if (dist < hiloSegLen)
            {
                changeDir = (segundoSeg.posNow - primerSeg.posNow).normalized;
            }

            Vector2 changeAmount = changeDir * error;

            if (i != 0)
            {
                primerSeg.posNow -= changeAmount * 0.5f;
                this.hiloSegmentos[i] = primerSeg;

                segundoSeg.posNow += changeAmount * 0.5f;
                this.hiloSegmentos[i + 1] = segundoSeg;
            }
            else
            {
                segundoSeg.posNow += changeAmount;
                this.hiloSegmentos[i + 1] = segundoSeg;
            }
        }
    }

    private void DrawRope()
    {
        float lineWidth = this.lineWidth;
        lineRenderer.startWidth = lineWidth;
        lineRenderer.endWidth = lineWidth;

        Vector3[] posicionesHilo = new Vector3[this.segmentoLength];

        for (int i = 0; i < this.segmentoLength; i++)
        {
            posicionesHilo[i] = this.hiloSegmentos[i].posNow;
        }

        lineRenderer.positionCount = posicionesHilo.Length;
        lineRenderer.SetPositions(posicionesHilo);
    }

    public struct HiloSegmento
    {
        public Vector2 posNow;
        public Vector2 posOld;

        public HiloSegmento(Vector2 pos)
        {
            this.posNow = pos;
            this.posOld = pos;
        }
    }
}

你有没有试过 Spring joint in unity,它可能足够逼真,你可以避免做很多硬编码。