• 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 OshakieGittens61 · Mar 24, 2019 at 09:47 PM · linerendererdrawingintersection

How to draw using line rendered in 3d space when a 3d character is moving?

Hey guys, so i have this script which draws closed shape using line rendered but it does not draw it in the XZ axis (3D). I want the a 3d character to draw it on the XZ plane which he moves. Heres my script and a quick setup my scene:

public class DrawLines : MonoBehaviour {

 private LineRenderer line;
 private bool isMousePressed;
 public List<Vector3> pointsList;
 private Vector3 mousePos;
 [SerializeField]
 private Transform player;
 public PlayerController playerController;
 private bool isGrounded;
 //private Vector3 

 // Structure for line points
 struct myLine
 {
     public Vector3 StartPoint;
     public Vector3 EndPoint;
 };
 //    -----------------------------------    
 void Awake ()
 {
     // Create line renderer component and set its property
     line = gameObject.AddComponent<LineRenderer> ();
     line.material = new Material (Shader.Find ("Particles/Additive"));
     line.SetVertexCount (0);
     line.SetWidth (0.1f, 0.1f);
     line.SetColors (Color.green, Color.green);
 //    line.useWorldSpace = true;    
     isMousePressed = false;
     pointsList = new List<Vector3> ();
     //        renderer.material.SetTextureOffset(
 }
 //    -----------------------------------    
 void Update ()
 {
     if(playerController.IsGrounded())
     {
         isMousePressed = true;
         line.SetVertexCount (0);
         pointsList.RemoveRange (0, pointsList.Count);
         line.SetColors (Color.green, Color.green);
     }

     // If mouse button down, remove old line and set its color to green
     if (Input.GetMouseButtonDown (0)) {
         isMousePressed = true;
         line.SetVertexCount (0);
         pointsList.RemoveRange (0, pointsList.Count);
         line.SetColors (Color.green, Color.green);
     }

     if (Input.GetMouseButtonUp (0)) {
         isMousePressed = false;
     }
     // Drawing line when mouse is moving(presses)
     if (isMousePressed) {
     //    mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, transform.position.z - Camera.main.transform.position.z));
         //mousePos = Camera.main.ScreenToWorldPoint(new Vector3(player.position.x,player.position.y, transform.position.z - Camera.main.transform.position.z));
         mousePos = player.position;
         //mousePos = Camera.main.ScreenToWorldPoint (player.position);
         mousePos.y = 0;
         if (!pointsList.Contains (mousePos)) {
             pointsList.Add (mousePos);
             line.SetVertexCount (pointsList.Count);
             line.SetPosition (pointsList.Count - 1, (Vector3)pointsList [pointsList.Count - 1]);
             if (isLineCollide ()) {
                 isMousePressed = false;
                 line.SetColors (Color.red, Color.red);
             }
         }
     }
 }
 //    -----------------------------------    
 //  Following method checks is currentLine(line drawn by last two points) collided with line 
 //    -----------------------------------    
 private bool isLineCollide ()
 {
     if (pointsList.Count < 2)
         return false;
     int TotalLines = pointsList.Count - 1;
     myLine[] lines = new myLine[TotalLines];
     if (TotalLines > 1) {
         for (int i=0; i<TotalLines; i++) {
             lines [i].StartPoint = (Vector3)pointsList [i];
             lines [i].EndPoint = (Vector3)pointsList [i + 1];
         }
     }
     for (int i=0; i<TotalLines-1; i++) {
         myLine currentLine;
         currentLine.StartPoint = (Vector3)pointsList [pointsList.Count - 2];
         currentLine.EndPoint = (Vector3)pointsList [pointsList.Count - 1];
         if (isLinesIntersect (lines [i], currentLine)) 
             return true;
     }
     return false;
 }
 //    -----------------------------------    
 //    Following method checks whether given two points are same or not
 //    -----------------------------------    
 private bool checkPoints (Vector3 pointA, Vector3 pointB)
 {
     return (pointA.x == pointB.x && pointA.y == pointB.y);
 }
 //    -----------------------------------    
 //    Following method checks whether given two line intersect or not
 //    -----------------------------------    
 private bool isLinesIntersect (myLine L1, myLine L2)
 {
     if (checkPoints (L1.StartPoint, L2.StartPoint) ||
         checkPoints (L1.StartPoint, L2.EndPoint) ||
         checkPoints (L1.EndPoint, L2.StartPoint) ||
         checkPoints (L1.EndPoint, L2.EndPoint))
         return false;

     return((Mathf.Max (L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min (L2.StartPoint.x, L2.EndPoint.x)) &&
         (Mathf.Max (L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min (L1.StartPoint.x, L1.EndPoint.x)) &&
         (Mathf.Max (L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min (L2.StartPoint.y, L2.EndPoint.y)) &&
         (Mathf.Max (L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min (L1.StartPoint.y, L1.EndPoint.y)) 
     );
 }

} alt text

scene2.png (112.9 kB)
scene3.png (154.9 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

0 Replies

· Add your reply
  • Sort: 

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

101 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

Related Questions

Adding a dot within bounds of sprite 1 Answer

3d drawing based on movable planes? 0 Answers

Suggestions for improvements of custom trail-drawing script highly appreciated. 0 Answers

3d object outline shader 1 Answer

Starting and Ending points of LineRenderer 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