Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aceofpack · Feb 21, 2016 at 09:55 AM · linerenderercoordinatesrecttransform

Drawing a line between 2 canvas RectTransform objects

I've been trying to draw a line (animated) between 2 points. The points being the center's of 2 Rectransforms as part of the UI - think, animating a line between 2 points on a game map for a 2D game.

What I have so far:

     private LineRenderer lineRenderer;
     private float lineDrawSpeed = 6f;
     private float dist;
     private float counter;    
 
 public RectTransform stage1Point;
     public RectTransform stage2Point;
     private Vector3 stage1Pos;
     private Vector3 stage2Pos;    
 
 void Start() {
 
         Camera camera = Grid.camera.GetCamera ();
 
 // I have tried a number of ways to try and convert these so it draws correctly but failing :(
         stage1Pos = stage1Point.position;
         stage2Pos = stage2Point.position;
 
         lineRenderer = GetComponent<LineRenderer> ();
         lineRenderer.SetPosition (0, stage1Pos);
         lineRenderer.SetPosition (1, stage2Pos);
         lineRenderer.SetWidth (0.3f, 0.3f);
         dist = Vector3.Distance (stage1Pos, stage2Pos);
 
 }
     void Update() {
 
         if (counter < dist) {
 
             counter += .1f / lineDrawSpeed;
             float x = Mathf.Lerp (0, dist, counter);
 
             Vector3 pointA = stage1Pos;
             Vector3 pointB = stage2Pos;
             Vector3 pointAlongTheLine = x * Vector3.Normalize (pointA - pointB) + pointA;
 
             lineRenderer.SetPosition (1, pointAlongTheLine);
 
         }
 
     }

The line draws, but goes off at z angles and strange positions. I'm pretty certain it's to do with the co-ordinate calculations but can't wrap my head around it. Cheers.

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

Answer by mikey555 · Feb 21, 2016 at 04:25 PM

I did it with a coroutine. It's a bit neater than putting things in Update! I recommend learning something about coroutines if you haven't. In a nutshell, the line 'return yield null' tells Unity to wait until the next frame to continue. This will draw a line between two points. You could modify it fairly easily to add more points.

 public class Lines : MonoBehaviour {
 
     LineRenderer lr;
 
     public RectTransform stage1;
     public RectTransform stage2;
     public float speed;
 
 
     void Awake()
     {
         lr = GetComponent<LineRenderer>();
     }
 
     void Start()
     {
         lr.SetVertexCount(2);
         lr.SetWidth(0.3f, 0.3f);
         StartCoroutine(AnimateLineBetween(stage1, stage2));
     }
 
 
     IEnumerator AnimateLineBetween(RectTransform a, RectTransform b)
     {
         // set first point
         lr.SetPosition(0, a.anchoredPosition3D);
         // initialize second point
         lr.SetPosition(1, a.anchoredPosition3D);
         
         // the distance (and direction) between the two points
         Vector3 distance = b.anchoredPosition3D - a.anchoredPosition3D;
         for (float i = 0; i < 1; i += speed / 200)
         {
             // each frame, advance a fraction of the way
             lr.SetPosition(1, distance * i);
             yield return null;
         }
     }
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Function ScreenPointToLocalPointInRectangle returns different position values. 1 Answer

Different materials in line renderer and antialiasing 0 Answers

LineRenderer draw every second line 2 Answers

Linerenderer circle not drawing as expected 3 Answers

Draw line in runtime 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