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 Reminator · Oct 18, 2011 at 06:18 PM · mouselinechoppy

Line render is choppy

Hi,

So what I'm trying to do is be able to click and drag the mouse to draw a line. I'm using the Line Renderer to create the line. It seems to be working, but as I drag the mouse completely vertical it looks like the rendering is incomplete and choppy. Going horizontally is fine. Below is my code and a picture of what it looks like.

alt text

 private var mouseLeftDownPoint : Vector2;
 private var mouseLeftUpPoint : Vector2;
 private var mouseDrag : boolean = false;
 private var raycastLength : float = 500;
 private var currentSelected : GameObject;
 private var movementPath : LineRenderer;
 private var numOfVertices : int = 1;
 private var vertexNo : int = 0;
 private var prevPnt : Vector3;
 private var lineTimer : float = 0;
 private var linePoints;
 
 function Start() 
 {
     movementPath = GetComponent(LineRenderer);
     prevPnt = Vector3(-1000, -1000, 0);
     linePoints = new Array(10000);
 }
 
 function Update() 
 {
     if(Input.GetButtonDown("Fire1")) 
     {
         MouseLeftDown(Input.mousePosition);
     }
 
     if (Input.GetButtonUp("Fire1")) 
     {
         MouseLeftUp(Input.mousePosition);
     }
 
     
 
     if (Input.GetButton("Fire1")) 
     {
         // Check to see if mouse is being dragged
 
         MouseLeftDrag(Input.mousePosition);
     }
 
     
 
     if(!mouseDrag)
     {
         lineTimer += Time.deltaTime;
         if(lineTimer > 1)
         {
             lineTimer = 0;
         }
     }
 }
 
 function MouseLeftDown(screenPosition : Vector2) 
 {
     mouseLeftDownPoint = screenPosition;    
 
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay(mouseLeftUpPoint);
 
     if(Physics.Raycast(ray, hit, raycastLength)) 
     {
         Debug.Log(hit.point);
         numOfVertices = 1;
         vertexNo = 0;
         movementPath.SetVertexCount(numOfVertices);
         movementPath.SetPosition(0, hit.point);
     }
 }
 
 function MouseLeftUp(screenPosition : Vector2) 
 {
     mouseLeftUpPoint = screenPosition;
     mouseDrag = false;
 
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay(mouseLeftUpPoint);
 
     if(Physics.Raycast(ray, hit, raycastLength)) 
     {
         Debug.Log(hit.point);
         // We hit a unit, now select it
         if(hit.collider.gameObject.name == "Spaceship")
         {
             currentSelected = hit.collider.gameObject;
             currentSelected.SendMessage("Selected");            
         }
     }
 }
 
 function MouseLeftDrag(screenPosition : Vector2)
 {
     if(screenPosition != mouseLeftDownPoint)
     {
         mouseDrag = true;
 
         var hit : RaycastHit;
         var ray = Camera.main.ScreenPointToRay(screenPosition);
 
         if(Physics.Raycast(ray, hit, raycastLength))
         {
             Debug.Log("hit point is " + hit.point);
             if(hit.point == prevPnt)
             {
                 // Don't do anything if the mouse is staying put
             }
             else
             {
                 movementPath.SetVertexCount(numOfVertices);
                 movementPath.SetPosition(vertexNo, hit.point);
                 numOfVertices++;
                 vertexNo++;
                 prevPnt = hit.point;
             }
         }
     }
 }
 
 @script RequireComponent(LineRenderer)

I'm not sure if it's the way I'm drawing it, or is it because the way Line Renderer works that it doesn't work well when rendering lines in real-time?

Any input is welcome :D.

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 Julien-Lynge · Oct 18, 2011 at 06:23 PM

Reminator, You are drawing the line renderer when your mouse projection hits a plane in scene. That means that the line you are creating occupies the same space as the plane, and what you're seeing is an effect known as Z-fighting. This video should clear things up for you:

http://www.burgzergarcade.com/tutorials/game-engines/unity3d/unity-ios-shaderlab-tutorial-10-1-z-fighting

Comment
Add comment · Show 1 · 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 Reminator · Oct 18, 2011 at 07:43 PM 0
Share

Haha, O$$anonymous$$, I can see how that problem occurs now. It's fixed now. Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to create a 2D line between a Game Object and the mouse? 0 Answers

How to make a ball shoot according to a line drawn 0 Answers

how to skethup lines at runtime with mouse position 1 Answer

Make a dotted line from an object to the mouse 2d 2 Answers

Moving line vertices with mouse 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