Home > OS >  my rope stretches infinitely when i put an object on it
my rope stretches infinitely when i put an object on it

Time:10-25

I have a rope generated by pure code in a script to do a fishing line in unity, the problem here is when i put the fishing buoy on the rope tip, the rope stretches infinitely and i want the buoy hanging, i tried changing the rope length and the segment sizes but it just keeps stretching, also i know that there is another way with the hinge joint 2d from unity, but i dont think its realistic enough to make a fishing line

Here is my code:

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

CodePudding user response:

Did you also try the Spring Joint in unity, it might be realistic enough and you could avoid doing a lot of hard coding.

  • Related