Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dannywaving · Aug 27, 2016 at 07:25 PM · colliderlinerenderer

LineRenderer & EdgeCollider problem

So I'm making a Unity game, one of its features being the player being able to draw a straight line with the LineRenderer. I also need to implement an EdgeCollider to the line created since it'll need to detect collisions with another object in the scene.

However, when I run the game, the line draws in the right place but the edge collider goes way off the screen and is much bigger than the line itself. Here's an example: alt text

Here's the code for line & edge collider, which is attached to the main camera (which is stationary):

 using UnityEngine;
 using System.Collections;
 
 public class LineHandler : MonoBehaviour {
 
     #region globals
     public Color c1 = Color.yellow; 
     public Color c2 = Color.red; // c1 and c2 set the color of the line
 
     private GameObject lineGO; // this game object holds the LineRenderer
     private LineRenderer lineRenderer; // LineRenderer instance
     private int i = 0; // integer used to store size and index of vertexes in the line
 
     private bool pcSet; // boolean used to confirm whether or not a bc is there
     PolygonCollider2D pc;
     Vector2 startPoint;
     #endregion
 
     #region methods
     void Start() // in this method we set up the necessary properties for the line
     {
         lineGO = new GameObject("Line"); // initialization of lineGO
         lineGO.AddComponent<LineRenderer>(); // we give lineGO the LineRenderer
         lineRenderer = lineGO.GetComponent<LineRenderer>(); // now lineRenderer knows what to refer to
         lineRenderer.material = new Material(Shader.Find("Mobile/Particles/Additive")); // material assignment
         lineRenderer.SetColors(c1, c2); // we set the start and end color of the line
         lineRenderer.SetWidth(0.1f, 0.1f); // we set the start and end width of the line 
         lineRenderer.SetVertexCount(0); // we set the number of points in the line, the more we set, the bigger the line can become
     }
 
     void Update() //the line is created here
     {
         if (Input.touchCount > 0) // the touchCount property returns the number of touches onscreen, so this block of code execs only if the screen is touched
         {
             Touch touch = Input.GetTouch(0); // the touch variable keeps a reference to the player's touch
 
             if(touch.phase == TouchPhase.Moved) // to move the line, the touch's phase needs to be Moved (Began/Moved/Stationary/Ended/Canceled)
             {
                 #region handleLine
                 if (i == 0) // we increase the max number of vertexes in the line to two (start & end)
                 {
                     lineRenderer.SetVertexCount(1); //at the very beginning though, the line must only have 1 vertex (a point)
                     startPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
                 }
                 else
                 {
                     lineRenderer.SetVertexCount(2);
                 }
 
                 Vector3 mPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,15); // mPosition is the position of the user's finger
                 lineRenderer.SetPosition(i, Camera.main.ScreenToWorldPoint(mPosition)); // i is the index of the point in question, mPosition is its position
 
                 if (i == 0)
                 {
                     i++; // the number of points increments only once
                 }
                 #endregion
 
                 #region handleCollider
                 if(!pcSet)
                 {
                     pc = lineGO.AddComponent<PolygonCollider2D>();
                     pcSet = true;
                 }
                 Vector2 endPoint = new Vector2(mPosition.x, mPosition.y);
                 Vector2 startPoint2 = new Vector2(startPoint.x,startPoint.y + 1);
                 Vector2 endPoint2 = new Vector2(endPoint.x,endPoint.y + 1);
                 pc.points = new Vector2[]{startPoint,endPoint,endPoint2,startPoint2};
                 #endregion
             }
 
             if(touch.phase == TouchPhase.Ended) // if the player removes his finger, this block executes
             {
                 #region destroyLine
                 lineRenderer.SetVertexCount(0); //deletes the block
                 i = 0; //reinitializes i
                 #endregion
 
                 #region destroyCollider
                 PolygonCollider2D[] lineColliders = lineGO.GetComponents<PolygonCollider2D>(); // creates an array with any Colliders in there
 
                 foreach(PolygonCollider2D p in lineColliders) //destroys all Colliders
                 {
                     Destroy(p);
                 }
 
                 pcSet = false; // like this i confirm that there is no collider
                 #endregion
             }
         }
     }
     #endregion
 }
 

I must also point out that for some reason the LineRenderer doesn't have a GetPosition, so I had to find a way to store the data from the start & end points of the lines.

Any advice on how to fix this? Thanks :D

capture.png (15.1 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Detecting if the object is hit by LineRenderer 1 Answer

Collider for Line Renderer? 5 Answers

Create a collider for area created by mouse 0 Answers

Internal collisions 1 Answer

Using ray cast does not produce reasonable results 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