• 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
2
Question by Tyler 2 · Dec 22, 2010 at 11:59 PM · line

Line renderer position?

Hello. When using the unity line renderer, is there a way to make the positions that the line uses as nodes to another objects transform position instead of a vector 3 position (I want the line to move)? Thanks

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

5 Replies

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

Answer by Jessy · Dec 23, 2010 at 12:09 AM

Use SetPosition in an Update() function, with the second parameter being the transform position of whatever you're tracking.

Comment
Add comment · Show 2 · 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 TechnoRazor · Sep 29, 2015 at 01:01 AM 0
Share

Does this not work in Javascript or what? I tried over and over to make it work, but I couldn't.

avatar image tlskillman · Sep 10, 2020 at 07:45 PM 0
Share

Two debug days later, found your post. Perfect. Thanks.

avatar image
11

Answer by seandanger · Feb 21, 2014 at 05:27 AM

Old question, but for future reference, you can uncheck the default "Use World Space" option in the inspector to have the Positions relative to the transform on the GameObject.

You can also set it in code as such (C# code on a MonoBehaviour):

 LineRenderer myLineRenderer = GetComponent<LineRenderer>();
 myLineRenderer.useWorldSpace = false;
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
avatar image
7

Answer by sj631 · Jan 20, 2017 at 08:48 AM

first set "use world space " to off in inspector set element 0 to 0,0,0 myLineRenderer.SetPosition(1,objectWithLineRenderer.transform.InverseTransformPoint(targetObject.transform.position)) if targetObject is moving put this in update or coroutine

Comment
Add comment · Show 2 · 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 Halocrafter · Mar 23, 2017 at 07:06 PM 1
Share

This worked perfectly for my needs. As a separate question though, what if I want multiple objects to have this effect? Would I need to make each object that'll have the effect have a line renderer as well, or is there a simpler way?

avatar image pratham2504 · Feb 26, 2018 at 07:47 PM 0
Share

I absolutely love you bro for this.. $$anonymous$$y days of remorse ends here.. Thanks mate.. :D

avatar image
1

Answer by NathanJSmith · Dec 26, 2018 at 11:36 AM

My solution is bake the line into mesh, then I can easily move that line

Here is the code:

 public static void BakeLineDebuger(GameObject lineObj)
 {
     var lineRenderer = lineObj.GetComponent<LineRenderer>();
     var meshFilter = lineObj.AddComponent<MeshFilter>();
     Mesh mesh = new Mesh();
     lineRenderer.BakeMesh(mesh);
     meshFilter.sharedMesh = mesh;
 
     var meshRenderer = lineObj.AddComponent<MeshRenderer>();
     meshRenderer.sharedMaterial = s_matDebug;
 
     GameObject.Destroy(lineRenderer);
 }

Make sure lineObj has LineRenderer component.

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
avatar image
0

Answer by Mickydtron · Dec 23, 2010 at 12:22 AM

You could make the line renderer a child object to the thing you want it to follow. Then it would automatically go everywhere the parent went.

Comment
Add comment · Show 2 · 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 drothenbucher · Feb 06, 2014 at 03:06 AM 1
Share

I can't seem to get that to work. It doesn't seem to follow the parent around. Is there something I might be forgetting to do. I created an empty game object with the line renderer and then made the game object a child of the object I wanted it to follow. the empty object follows the parent but the lines do not.

avatar image drudiverse · Jul 04, 2014 at 10:55 AM 1
Share

doesnt work, setposition is independent of parent transform.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Draw a line via script and make a solid with it.. 1 Answer

Drawing a glowing line from one point to another on update 1 Answer

Line Renderer and Collider Question 0 Answers

How to move an enemy on a curvy line? 4 Answers

How would I replicate Gizmos.DrawLine in-game? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges