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 alexander11 · Aug 07, 2016 at 01:30 AM · c#unity 53dlinerenderer

How to render a LineRenderer through multiple points?

Hello i am having a little trouble trying to render Lines through multiple points but the only thing i'm getting is whats in the picture, its only the start point to 0,0,0 anyone know how to fix this?

Here is my code so you know what i have done.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(LineRenderer))]
 public class Lines : MonoBehaviour {
 
     LineRenderer lineRenderer;
     public Transform[] points;
 
     void Update () {
         lineRenderer = GetComponent<LineRenderer>();
         for (int i = 0; i < points.Length; i++)
             lineRenderer.SetPosition(0, points[0].position);
     }
 }


alt text

capture01.png (37.4 kB)
capture01.png (36.6 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

3 Replies

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

Answer by alexander11 · Aug 07, 2016 at 02:33 AM

OK i have fixed it here is the code.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(LineRenderer))]
 public class Lines : MonoBehaviour {
 
     LineRenderer lineRenderer;
     public Transform[] points;
     private Vector3[] vP;
     int seg = 20;
     void Start()
     {
         lineRenderer = GetComponent<LineRenderer>();
         Line();
     }
     void Line () {
         seg = points.Length;
         vP = new Vector3[points.Length];
         for (int i = 0; i < points.Length; i++)
         {
             vP[i] = points[i].position;
         }
             for (int i = 0; i < seg; i++)
             {
                 float t = i / (float)seg;
                 lineRenderer.SetVertexCount(seg);
                 lineRenderer.SetPositions(vP);
             }
 
     }
 }
 
Comment
Add comment · Show 3 · 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 StormMuller · Aug 07, 2016 at 02:36 AM 0
Share
 float t = i / (float)seg;

What's this line's purpose?

avatar image alexander11 StormMuller · Aug 07, 2016 at 02:44 AM 0
Share

For the amount of vertex/vertices, then i put in the lineRenderer.SetVertexCount. it's practically the Segment count.

avatar image alexander11 StormMuller · Aug 07, 2016 at 02:51 AM 0
Share

since i have achieved this for the LineRenderer, would you know how to spawn object's in between the all of the points? this would be helpful because i am trying to create nodes so i can render a road.

avatar image
3

Answer by StormMuller · Aug 07, 2016 at 02:06 AM

You have 3 problems here.

  1. You are not using the i variable made in the loop, so you are only using the first position. To fix this replace lineRenderer.SetPosition(0, points[0].position); with lineRenderer.SetPosition(0, points[i].position);

  2. You are using SetPosition instead of SetPositions

  3. doing this whole computation in the update function is really inefficient.

So this is what I recommend you do:

 Using System.Collections.Generic;
 
  [RequireComponent(typeof(LineRenderer))]
  public class Lines : MonoBehaviour {
  
      LineRenderer lineRenderer;
      public Transform[] points;
      private Vector3[] positionsOfPoints;
 
 void Start(){
     lineRenderer = GetComponent<LineRenderer>();
     UpdateLine(); // this can be called in update if your positions aren't static
 }
 
 void UpdateLine()
 {
     List<Vector3> temp = new List<Vector3>():
     foreach(Transform t in points)
     {
          temp.Add(t.position);
     }
 
     positionsOfPoints = temp.ToArray();
     lineRenderer.SetVertexCount(positionsOfPoints.Length); // add this
     lineRenderer.SetPositions(positionsOfPoints);
   }
 }

I haven't tested this yet but it seems correct. :)

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 alexander11 · Aug 07, 2016 at 02:13 AM 0
Share

It semi works its only rendering the line Renderer from point 1 to point 2 but not to the other points.

And i am getting an Error. LineRenderer.SetPosition index out of bounds

avatar image StormMuller · Aug 07, 2016 at 02:26 AM 0
Share

Updated my answer. check the second last line of code (Line 24).

avatar image
0

Answer by Firedan1176 · Aug 07, 2016 at 01:58 AM

You're not using your iteration index in your for loop. Change points[0].position to points[i].position.

Comment
Add comment · Show 3 · 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 alexander11 · Aug 07, 2016 at 02:06 AM 0
Share

that still does not work its now 0,0,0 to the last point.

avatar image StormMuller · Aug 07, 2016 at 02:15 AM 0
Share

Have you set the points field in the inspector?

avatar image alexander11 StormMuller · Aug 07, 2016 at 02:22 AM 0
Share

nope what is field anyway? i'm still a rookie in c#

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

220 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 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

How do i cast mesh like a linerenderer? 0 Answers

How do i access the MeshExtrusion.cs from Unity's PE? 1 Answer

How do i create objects in between two points with an offset??? 1 Answer

How to move 3D character with acceleration and deceleration and face in direction of movement 0 Answers

Door animation bugs and stays open unity 3D 0 Answers


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