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
1
Question by mikhailovic · Aug 12, 2014 at 02:58 AM · c#colliderline renderer

How can i add a collider to my line renderer script?

I have bit of code here that by holding, dragging and releasing my mouse button can make me draw lines in game. What i need help with is how i can add a type of collider to this. Help would be appreciated!

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DrawLine : MonoBehaviour 
 {
     private LineRenderer line;
     private bool isMousePressed;
     private List<Vector3> pointsList;
     private Vector3 mousePos;
     
     // 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>();
     }
     //    -----------------------------------    
     void Update () 
     {
         // 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);
         }
         else if(Input.GetMouseButtonUp(0))
         {
             isMousePressed = false;
         }
         // 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]);
             }
         }
     }
 
 }
Comment
Add comment · Show 2
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 robertbu · Aug 12, 2014 at 03:21 AM 0
Share

http://answers.unity3d.com/questions/418759/how-might-i-go-about-making-a-trail-render-that-de.html

http://answers.unity3d.com/questions/285040/draw-a-line-in-game.html

avatar image mikhailovic · Aug 12, 2014 at 02:22 PM 0
Share

I could need more help changing my current code.

4 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by ZackOfAllTrades · Jun 06, 2017 at 07:54 AM

Thanks for all the suggestions, I had trouble with them though. Here is a quick tutorial on how I solved this problem. First a list of what we need, write these down and fill them in:

startPoint of the line

endPoint of the line

lineLength which between these two points

lineWidth which you can get from the line renderer by calling YourLineRendererHere.endWidth

midPoint which is simply (startPoint + endPoint) / 2

slope which recall is (y2 - y1)/(x2-x1)

Thats It!

let's get the slope

y2 = endPoint.z

y1 = startPoint.z

x2= endPoint.x

x1 = startPoint.x

lets plug it in, don't calculate this just leave it slope = (endPoint.z - startPoint.z)/ (endPoint.x - startPoint.x)

!!! IMPORTANT !!!

In 3d space you use the x and z axis becuase the y is pointing up and we don't care about that. In 2d space you use the x and y axis, you would also use Vector2 points in your function not Vector 3

Here's your function--

  private void AddColliderToLine(LineRenderer line, Vector3 startPoint, Vector3 endPoint)
     {
     //create the collider for the line
         BoxCollider lineCollider = new GameObject("LineCollider").AddComponent<BoxCollider>();
         //set the collider as a child of your line
         lineCollider.transform.parent = line.transform; 
     // get width of collider from line 
     float lineWidth = line.endWidth; 
     // get the length of the line using the Distance method
         float lineLength = Vector3.Distance(startPoint, endPoint);      
         // size of collider is set where X is length of line, Y is width of line
         //z will be how far the collider reaches to the sky
         lineCollider.size = new Vector3(lineLength, lineWidth, 1f);   
     // get the midPoint
         Vector3 midPoint = (startPoint + endPoint) / 2;
     // move the created collider to the midPoint
         lineCollider.transform.position = midPoint;
 
 
        //heres the beef of the function, Mathf.Atan2 wants the slope, be careful however because it wants it in a weird form
     //it will divide for you so just plug in your (y2-y1),(x2,x1)
     float angle = Mathf.Atan2((endPoint.z - startPoint.z), (endPoint.x - startPoint.x));
     
     // angle now holds our answer but it's in radians, we want degrees
     // Mathf.Rad2Deg is just a constant equal to 57.2958 that we multiply by to change radians to degrees
         angle *= Mathf.Rad2Deg;
 
     //were interested in the inverse so multiply by -1
         angle *= -1; 
     // now apply the rotation to the collider's transform, carful where you put the angle variable
     // in 3d space you don't wan't to rotate on your y axis
         lineCollider.transform.Rotate(0, angle, 0);
     }


Thanks for checking the tutorial out!!!

Heres the same thing with pictures: http://zackgrizzle.ninja/tutorials.html

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 Seb1101 · Feb 21, 2019 at 02:11 PM 0
Share

Thanks for that great answer! Works like a charm!

avatar image
3

Answer by Swati Patel · Mar 09, 2015 at 09:21 PM

Hey mikhailovic your code looks similar to my blog http://www.theappguruz.com/tutorial/draw-line-mouse-move-detect-line-collision-unity2d-unity3d/.

Now to add collider to this line, check this http://www.theappguruz.com/unity/add-collider-to-line-renderer-unity/

Hope it will help you.

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
1

Answer by bertrand_arnaud · Apr 11, 2019 at 12:36 PM

Since Unity 2018.2, you can now use the public method BakeMesh of LineRenderer. This will give you the mesh along the LineRenderer with all the segments. You can then use this mesh with a mesh collider in order to detect Collisions and Raycasts.

 LineRenderer lineRenderer = line.GetComponent<LineRenderer>();
 MeshCollider meshCollider = line.AddComponent<MeshCollider>();
 
 Mesh mesh = new Mesh();
 lineRenderer.BakeMesh(mesh, true);
 meshCollider.sharedMesh = mesh;

I tested it and it worked on my project. Let me know what you think about it and if it helps you !

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 johnnysc22 · Apr 28, 2019 at 11:19 AM 0
Share

This does not work if you need to use a Trigger since this will create a Concave mesh which doesn't support triggers.

I'm brand new to Unity so any help/advice would be appreciated.

avatar image jamesstephenbrown · Aug 31, 2019 at 07:20 AM 0
Share

Thanks, that was ridiculously easy.

avatar image
0

Answer by bugz313 · Jan 31, 2016 at 12:57 AM

You can try my plugin: https://www.assetstore.unity3d.com/en/#!/content/32762,You can try this: https://www.assetstore.unity3d.com/en/#!/content/32762

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

BoxCollider Overlap OnTriggerEnter Problem 1 Answer

Drag a sprite by its 2D Polygon Collider component 1 Answer

C# Slow Down all Gameobjects Within another Gameobject's Collider 2 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