-2

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;
        }
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
sergiogix
  • 3
  • 1

1 Answers1

0

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

error13660
  • 120
  • 7
  • i searched it up and youre right, its pretty simple and effective but can i give that property a skin for the rope?? – sergiogix Oct 24 '21 at 18:40
  • I suggest you use a thing called a Line Renderer, you can give it two points and it connects them with a sprite, it is quite simple to use and it might be the effect you are looking for. In case you want to use curved lines, it's a bit more difficult, you would have to use bézer curves, which is a matemathical formula to create any shape of curve you want. But it might be a bit difficult for you right now. (If you still decide to use them, you can specify points on a bezier curve and connect those with straight lines, you you get your curved line) – error13660 Oct 25 '21 at 14:48
  • I did the LineRenderer thing with the code above which uses a mathematical formula too, making a startPoint and Endpoint follow the 2 objects of the spring joint and it works! I didnt get the bézer curves thing but ill give it a try when i get the essential parts so thank you so much :) – sergiogix Oct 25 '21 at 15:41
  • https://www.youtube.com/watch?v=aVwxzDHniEw&t=449s Here is a video about bézier curves that might help – error13660 Oct 25 '21 at 16:15