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 ratchetunity · Jul 28, 2020 at 04:09 PM · linerendererlineline renderercurvesspring

Line renderer spring shape

Hi! I'm trying to connect two gameobjects using a linerenderer with a spring shape. I'm facing two issues: -The shape is created, but it doesn't connect the first object with the second one. -The first and last vertex of the linerenderer doesn't fit the first and last object center (see the screenshot)

This is the code I'm using:

 public Transform object1;
 public Transform object2;
 private LineRenderer lineRenderer;
 const int NUMBER_OF_POINTS = 100;

 private void Awake()
 {
     lineRenderer = GetComponent<LineRenderer>();
     lineRenderer.positionCount = NUMBER_OF_POINTS;
 }

 private void Start()
 {
     CreateCurveShape();
 }

 private void CreateCurveShape()
 {
     float segmentWidth = Vector3.Distance(object1.position, object2.position) / NUMBER_OF_POINTS;
     Vector3[] points = new Vector3[NUMBER_OF_POINTS];
     for (int i = 0; i < NUMBER_OF_POINTS; i++)
     {
         float z = segmentWidth * i;
         float x = Mathf.Sin(z);
         float y = Mathf.Cos(z);
         points[i] = new Vector3(x, y, z);
     }
     lineRenderer.SetPositions(points);
 }

alt text

screenshot.png (4.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Shrimpey · Jul 29, 2020 at 10:09 AM

Not sure if you need 2D or 3D solution, so I just made the most universal one that applies to 3D space and can probably be somewhat used in 2D too.

There are a lot of ways to go about it (the beauty of algebra), I created a plane with normal pointing towards second object (from first one) and then created points on that plane moving it along the normal axis:

 public Transform object1;
     public Transform object2;
     private LineRenderer lineRenderer;
     const int NUMBER_OF_POINTS = 100;
     const int NUMBER_OF_REVOLUTIONS = 5;
     const float SPIRAL_WIDTH = 2.0f;
     private void Awake() {
         lineRenderer = GetComponent<LineRenderer>();
         lineRenderer.positionCount = NUMBER_OF_POINTS;
     }
     private void Start() {
         CreateCurveShape();
     }
     private void CreateCurveShape() {
         float objectDistance = Vector3.Distance(object1.position, object2.position);
         Vector3[] points = new Vector3[NUMBER_OF_POINTS];
         for (int i = 0; i < NUMBER_OF_POINTS; i++) {
             // Define plane with normal pointing to second object
             Vector3 planeForwardDirection = (object2.position - object1.position);
             Vector3 planeRightDirection = Quaternion.Euler(90.0f, 0.0f, 0.0f) * planeForwardDirection;
             Vector3 planeUpDirection = Vector3.Cross(planeForwardDirection, planeRightDirection);
 
             // Some additional debug to visualize our plane
             Debug.DrawLine(object1.position, planeForwardDirection, Color.red, 1000);
             Debug.DrawLine(object1.position, planeUpDirection, Color.cyan, 1000);
             Debug.DrawLine(object1.position, planeRightDirection, Color.green, 1000);
 
             // Distance along the object to object axis
             float currentDistanceNormalized = (float)i / (float)NUMBER_OF_POINTS;
             float currentDistance = currentDistanceNormalized * objectDistance;
 
             // New coords on our plane facing the second object
             Vector3 spiralCoords = SPIRAL_WIDTH * planeUpDirection.normalized * Mathf.Sin(NUMBER_OF_REVOLUTIONS * currentDistanceNormalized * 2 *Mathf.PI) +
                                    SPIRAL_WIDTH * planeRightDirection.normalized * (Mathf.Cos(NUMBER_OF_REVOLUTIONS * currentDistanceNormalized * 2 *Mathf.PI) - 1.0f) +
                                    planeForwardDirection.normalized * currentDistance;
             points[i] = spiralCoords;
         }
         lineRenderer.SetPositions(points);
     }

It should work fine no matter how you position your objects in 3D space, maybe some minor modifications might be needed for your specific application. I also threw in number of revolutions and width variables.

Of course the debug lines can be removed. The -1.0f Mathf.Cos moves the starting/ending point to the object (cosine of 0 is 1, so we need to offset it to match our object's position).

Comment
Add comment · Show 1 · Share
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
avatar image ratchetunity · Aug 06, 2020 at 08:18 AM 0
Share

It worked! Thank you so much. $$anonymous$$inor modifications are needed yes, but it's just a tweak depending on the project as you said (for example, if the scale between the objects is not the same)

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

131 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

Related Questions

LineRenderer lagging behind 1 Answer

Draw out Line in C# 0 Answers

Achieve a "stroke" / "transparent" effect with the Line Renderer in 2D 0 Answers

What's the best way to draw a 2D line WITHOUT using LineRenderer? 1 Answer

How can I Disable all but one renderer in an array? 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