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 Darkskxcher · Jan 22, 2015 at 12:40 PM · c#linerendererwaypoint

Draw lines between waypoints (using lineRenderer at the moment)

Hello guys, I am trying to generate lines between waypoints. The thing I am trying to achieve is to draw a line from waypoint 1 to 2 and when your reach waypoint 2 it removes the lines between 1 and 2 and creates a new line between 2 and 3 and so on.

This is the code I have for my waypoints at the moment but I just can't seem to incorporate a linerenderer without getting lots of errors.

  using UnityEngine;
     using System.Collections;
     
     public class Waypoints : MonoBehaviour
     {
             private Transform startMarker, endMarker;
             public Transform[] waypoint; 
             private float speed = 1.0F;
             private float startTime;
             private float journeyLength;
         //    public Transform target;
             private int currentStartPoint;
     
             void Start ()
             {
                     currentStartPoint = 0;
                     SetPoints (); 
             }
         
             void SetPoints ()
             {
                     startMarker = waypoint [currentStartPoint];
                     endMarker = waypoint [currentStartPoint + 1];
                     startTime = Time.time;
                     journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
             }
             void Update ()
             {
                     float distCovered = (Time.time - startTime) * speed; 
                     float fracJourney = distCovered / journeyLength;
                     transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
                     if (fracJourney >= 1f && currentStartPoint + 1 < waypoint.Length) {
                             currentStartPoint++;
                             SetPoints ();
                     }
                     
             }
     }
 
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
2
Best Answer

Answer by jenci1990 · Jan 22, 2015 at 07:10 PM

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(LineRenderer))]
 public class Waypoints : MonoBehaviour {
     private Transform startMarker, endMarker;
     public Transform[] waypoint;
     private float speed = 2f;
     private float startTime = 0f;
     private float journeyLength = 1f;
     private int currentStartPoint = 0;
     private LineRenderer lineRenderer;
     void Start() {
         currentStartPoint = 0;
         SetPoints();
         lineRenderer = GetComponent<LineRenderer>();
     }
     void SetPoints() {
         startMarker = waypoint[currentStartPoint];
         endMarker = waypoint[currentStartPoint + 1];
         startTime = Time.time;
         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
     }
     void Update() {
         float distCovered = (Time.time - startTime) * speed;
         float fracJourney = distCovered / journeyLength;
         Vector3 startPosition = startMarker.position;
         Vector3 endPosition = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
         lineRenderer.SetPosition(0, startPosition);
         lineRenderer.SetPosition(1, endPosition);
         if (fracJourney >= 1f) {
             if (currentStartPoint + 2 < waypoint.Length) {
                 currentStartPoint++;
                 SetPoints();
             } else {
                 //if finished, disable lineRenderer and this script
                 lineRenderer.enabled = false;
                 this.enabled = false;
             }
         }
     }
 }
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 Darkskxcher · Jan 23, 2015 at 08:42 AM 0
Share

Thank you so much, this was exactly what I needed! I tweaked it around and now it works perfect.

avatar image rajavamsidhar_gvs · Jun 04, 2015 at 08:06 AM 0
Share

thanks for the code..

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Draw shortest distance between 2 points on a sphere 1 Answer

Line Renderer Issues 1 Answer

Need an advise to upgrade my code!!! 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