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 sarthakshah · Sep 08, 2014 at 04:56 AM · linerendererfollow path

Trouble in following path of line renderer...

Hi, I have written code of drawing line using line renderer by mouse drag, path is drawn, and then i want to follow mycube as per it with smoothly, my cube can follow for the first drag, if i draw, one more time, then it is not able to follow smoothly, for that i have written below code , please suggest me what i am doing wrong?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Vectrosity;
 
 public class DrawLine : MonoBehaviour 
 {
     VectorLine myLine1;
     VectorLine recentLine;
     public Material lineMat;
 
 
     private float startTime;
     private float journeyLength;
     public float speed = 1.0F;
     public Vector3 lastPos;
     Vector3[] myRoute;
 
     private LineRenderer line;
     private bool isMousePressed;
     private List<Vector3> pointsList;
     private Vector3 mousePos;
     private bool startLineAnimation;
     // Structure for line points
     struct myLine
     {
         public Vector3 StartPoint;
         public Vector3 EndPoint;
     };
     //    -----------------------------------    
     void Awake()
     {
         line = gameObject.GetComponent<LineRenderer> ();
         line.SetVertexCount(0);
         line.material = lineMat;
         line.renderer.enabled = true;;
         line.useWorldSpace = true;    
         isMousePressed = false;
         startLineAnimation = false;
         pointsList = new List<Vector3>();
     }
 
 
     //    -----------------------------------    
     void Update () 
     {
         // If mouse button down, remove old line and set its color to green
         if(Input.GetMouseButtonDown(0))
         { 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100))
             {
                 if (hit.transform.gameObject == transform.gameObject)
                 {
                 isMousePressed = true;
                 line.SetVertexCount(0);
                 pointsList.RemoveRange(0,pointsList.Count);
                 }
             }
             //line.SetColors(Color.green, Color.green);
         }
         else if(Input.GetMouseButtonUp(0))
         {
             if(isMousePressed)
             {
             isMousePressed = false;
             DrawMe();
             }
         }
         // Drawing line when mouse is moving(presses)
         if(isMousePressed)
         {
             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             mousePos.z=0;
             if (!pointsList.Contains (mousePos)) 
             {
                 pointsList.Add (mousePos);
                 line.SetVertexCount (pointsList.Count);
                 line.SetPosition (pointsList.Count-1 , (Vector3)pointsList [pointsList.Count-1]);
 
 //                StartCoroutine(add (mousePos));
 //                if(isLineCollide())
 //                {
 //                    isMousePressed = false;
 //                    //line.SetColors(Color.red, Color.red);
 //                }
             }
         }
         if (startLineAnimation && recentLine.active) {
 
                         recentLine.textureOffset = -Time.time * 2.0f % 1f;
 
                         StartCoroutine(StartFollowing());
 
                 }
 
     }
 
     IEnumerator StartFollowing () {
                 
                 int  counter = 0;
                 Vector3 pointA = transform.position;
                 Vector3 pointB = myRoute[counter];
                 print ("pointA:"+pointA);
                 print ("pointB:"+pointB);
                 while (counter<=myRoute.Length-1) {
                         yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
                         counter++;
                         if(counter<=myRoute.Length-1)
                             {
                             pointA=pointB;
                             pointB = myRoute[counter];
                             }
 
                         //yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
                     }
             }
 
     IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) {
                 var i= 0.0f;
                 var rate= 2.0f/time;
                 while (i < 1.0f) {
                     i += Time.deltaTime * rate;
                     thisTransform.position = Vector3.Lerp(startPos, endPos, i);
                     yield return null; 
                 }
             }
 
 
     public void DrawMe()
     {
         line.enabled = false;
 
          myRoute = new Vector3[pointsList.Count];
         int i ;
         for (i=0; i<pointsList.Count; i++) 
         {
 
             myRoute[i] = pointsList[i];
             print(i+" : "+myRoute[i]);    
         
         }
     
          lastPos = myRoute[i-1];
         print (lastPos);
         VectorLine.Destroy(ref myLine1);
 
         myLine1 = new VectorLine ("Line1", myRoute, lineMat, 2.5f, LineType.Continuous);
         myLine1.textureScale = 5.0f;
         recentLine = myLine1;
         myLine1.Draw ();
 
         startLineAnimation = true;
 
         //for (int i=0; i<myRoute.Length; i++) 
         //{
         startTime = Time.time;
         journeyLength = Vector3.Distance(transform.position, lastPos);    
 
             
         //}
 
         /*myLine1 = VectorLine.SetLine (Color.cyan, myRoute);
         myLine1.smoothWidth = true;
         myLine1.material = lineMat;*/
     }
 
 }

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
1
Best Answer

Answer by sarthakshah · Sep 30, 2014 at 08:55 AM

Hi,

Got the solution of it.

By just replacing following two lines,

mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z=0;

By this line

 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, transform.position.z - Camera.main.transform.position.z));


Cheers......

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

1 Person is following this question.

avatar image

Related Questions

Collider is not working... 2 Answers

How draw a path using touch in c# 1 Answer

Instantiate LineRenderer on the network 2 Answers

Line renderer is not working for Perspective camera. 1 Answer

Lighting up Linerenderers 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