• 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 /
  • Help Room /
avatar image
0
Question by Ego65 · Nov 20, 2015 at 06:47 PM · line renderer

line renderer jumps when start and end positons belong to moving objects

i'm working on a 3d radar for my space game. i wrote a (simple) script for a line renderer which uses my enemy-radar-cube as startpos. and my player-radar-cube as its endpos. it works but not really correctly. seems to me that the pos-calcuting wont be done fast enough the faster my ship moves or whatever. i dont know anymore how to fix this :(

 using UnityEngine;
 
 public class eny_line_renderer : MonoBehaviour {
 
     public  GameObject thisobject;  // my enemy radar cube
     public GameObject center;       // my player radar cube
     public float startwidth;
     public float endwidth;
 
     public Color c1 = Color.yellow;
     public Color c2 = Color.yellow;
     public Color c3 = Color.green;
     public Color c4 = Color.green;
 
     public Vector3 pos_start_pt;
     public Vector3 pos_end_pt;
 
     private float thisobject_x;
     private float thisobject_y;
     private float thisobject_z;
 
     // private float center_x;
     private float center_y;
     private float center_z;
 
     // Update is called once per frame
     void Update ()
     {}
     void LateUpdate()
     {
         LineRenderer lineRenderer = GetComponent<LineRenderer>();
         lineRenderer.SetWidth(startwidth, endwidth);
         lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
         //center_x = center.transform.position.x;
         center_y = center.transform.position.y;
         center_z = center.transform.position.z;
 
         thisobject_x = thisobject.transform.position.x;
         thisobject_y = thisobject.transform.position.y;
         thisobject_z = thisobject.transform.position.z;
 
         pos_start_pt = new Vector3(thisobject_x, center_y, thisobject_z);
         pos_end_pt = new Vector3(thisobject_x, thisobject_y, thisobject_z);
 
         lineRenderer.SetPosition(0, pos_start_pt);
         lineRenderer.SetPosition(1, pos_end_pt);
 
         if (thisobject_z >= center_z)        // in front of 
         {
             lineRenderer.SetColors(c3, c4); // = green
         }
         else                                // behind of 
         {
             lineRenderer.SetColors(c1, c2); // = yellow
         }
     }
 }
 

 
Comment
Add comment · Show 3
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 rutter · Nov 20, 2015 at 06:48 PM 0
Share

What problem are you actually seeing? You can certainly update the LineRenderer positions once per frame, so I'm inclined to assume the problem is more with the positions that you're sending to it.

avatar image OncaLupe · Nov 21, 2015 at 02:32 AM 1
Share

It may have something to do with how the objects are being moved, so would need to see that code before being able to say anything with certainty. However I do know that the code you've shown is more complex than needed, and the first three lines of the LateUpdate really should be done in Awake/Start.

 using UnityEngine;
 
 public class eny_line_renderer : $$anonymous$$onoBehaviour {
     
     public  GameObject thisobject;  // my enemy radar cube
     public GameObject center;       // my player radar cube
     public float startwidth;
     public float endwidth;
     LineRenderer lineRenderer;
     
     public Color c1 = Color.yellow;
     public Color c2 = Color.yellow;
     public Color c3 = Color.green;
     public Color c4 = Color.green;
     
     public Vector3 pos_start_pt;
 
     void Awake ()
     {
         //GetComponent is slow. If needed more than once, reference should be stored.
         lineRenderer = GetComponent<LineRenderer>();
         lineRenderer.SetWidth(startwidth, endwidth);
         lineRenderer.material = new $$anonymous$$aterial(Shader.Find("Particles/Additive"));
     }
 
     void LateUpdate()
     {
 
         pos_start_pt.x = thisobject.transform.position.x;
         pos_start_pt.y = center.transform.position.y;
         pos_start_pt.z = center.transform.position.z;
 
         lineRenderer.SetPosition(0, pos_start_pt);
         lineRenderer.SetPosition(1, thisobject.transform.position);
         
         if (thisobject.transform.position.z >= center.transform.position.z)        // in front of 
         {
             lineRenderer.SetColors(c3, c4); // = green
         }
         else                                // behind of 
         {
             lineRenderer.SetColors(c1, c2); // = yellow
         }
     }
 }
avatar image Ego65 · Nov 21, 2015 at 01:25 PM 0
Share

afterwards : my enemy radar cube is a child of the enemy game object =( public game object thisobject) my player radar cube is a child of the player game object =(public GameObject center)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ego65 · Nov 21, 2015 at 06:42 AM

thx for your quick answers ! now that you've mentioned it really seems they "jump" from an old pos. to a new pos. i've tried yours but the problem is still there. Have to say that i'm (still) a beginner and so may i please you for help in : how to store a line rend. reference and if it will be necessary how to store a recent position and calculating the new pos ?
P.S.: i'm not really familar in vector mathematics so although this kind of script seems to be more complex its easier to comprehend calculating positions for each axes than to work with vector commands i dont really know what they are "doing".

to make things a bit clearer here is my script which i attached to the enemy:

 using UnityEngine;
 
 public class Eny_rad_follow : MonoBehaviour
 {
     public GameObject Player;
     public GameObject RadarCenter;
     public GameObject enemy;
     public GameObject eny_rad_cube;
 
     public Transform rad_transform; // the eny_rad_cube transform
 
     // the limit of the distance the eny_rad_cube moves  
     public float range = 2.0f;
    
     private float player_X;
     private float player_Y;
     private float player_Z;
 
     private float enemy_X;
     private float enemy_Y;
     private float enemy_Z;
 
     private float orig_dist_X;
     private float orig_dist_Y;
     private float orig_dist_Z;
 
     private float radar_dist_X;
     private float radar_dist_Y;
     private float radar_dist_Z;
 
     private float rad_Cent_X;
     private float rad_Cent_Y;
     private float rad_Cent_Z;
 
     private float eny_rad_cube_X;
     private float eny_rad_cube_Y;
     private float eny_rad_cube_Z;
 
     private float newPos_X;
     private float newPos_Y;
     private float newPos_Z;
 
     void LateUpdate()
     {
         player_X = Player.transform.position.x;
         player_Y = Player.transform.position.y;
         player_Z = Player.transform.position.z;
 
         enemy_X = enemy.transform.position.x;
         enemy_Y = enemy.transform.position.y;
         enemy_Z = enemy.transform.position.z;
 
         orig_dist_X = (player_X - enemy_X);
         orig_dist_Y = (player_Y - enemy_Y);
         orig_dist_Z = (player_Z - enemy_Z);
 
         rad_Cent_X = RadarCenter.transform.position.x;
         rad_Cent_Y = RadarCenter.transform.position.y;
         rad_Cent_Z = RadarCenter.transform.position.z;
 
         if (orig_dist_X >= range)
         {
             orig_dist_X = range;
         }
 
         if (orig_dist_X <= -range)
         {
             orig_dist_X = -range;
         }
 
         if (orig_dist_Y >= range)
         {
             orig_dist_Y = range;
         }
 
         if (orig_dist_Y <= -range)
         {
             orig_dist_Y = -range;
         }
 
         if (orig_dist_Z >= range)
         {
             orig_dist_Z = range;
         }
         if (orig_dist_Z <= -range)
         {
             orig_dist_Z = -range;
         }
 
         radar_dist_X = (orig_dist_X * 0.2f);  // *0.2 reduces the  original speed of the enemy obj 
         radar_dist_Y = (orig_dist_Y * 0.2f);  // to the enymy radar cube
         radar_dist_Z = (orig_dist_Z * 0.2f); //  just my personal impression but seems to fit well
 
         newPos_X = (rad_Cent_X - radar_dist_X) ;
         newPos_Y = (rad_Cent_Y - radar_dist_Y) ;
         newPos_Z = (rad_Cent_Z - radar_dist_Z);
 
         rad_transform.position = new Vector3((newPos_X), (newPos_Y), (newPos_Z));
     }
 }


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 Ego65 · Nov 23, 2015 at 12:39 PM

Updated: after going on with "my game" the same prob happened again. It turned out that the problem was that the radar cube has been a child of my enemy ship. Seems the position wasn't updated fast enough so i made it independant. as well as its doesnt rotate anymore accordingly to the enemy ship which isnt necessary in the "radar". i set the code back in the update function and now its working perfectly :)"

alt text


screenshot.png (426.6 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to draw a line in 2D with touch that has collider?? 0 Answers

Line renderer alignment not property missing , local alignmenent not showing 0 Answers

Line Renderer issue 0 Answers

Drawing line on UI and transform it 3d line 0 Answers

Cant manually change color of line renderer 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