Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sergiogix · Oct 11, 2021 at 09:27 AM · unity 2dgravityrope

how can i limit my rope?

I have a rope generated by pure code in a script to do a fishing line, the problem here is when i put the fishing buoy on the rope tip, the rope stretches infinitely and i want the buoy hanging

Thanks in advance

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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Verlet Elastic Collision 0 Answers

How to check if a enemy is moving up or down? 1 Answer

How to build upon Jongallant's verlet integration rope project 0 Answers

How can I allow the rope to correctly interact with Box and Polygon colliders? 0 Answers

Issue to destroy objects using RaycastHit2D when instantiated from 2D Sprite and they are in motion, i.e. falling against gravity? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges