• 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 joan · Nov 20, 2013 at 04:11 PM · colliderdrawline

Vectrosity line with collider?

I´m tryng to make a game with drawing lines, with one question, the line can not cross it self. I´m using vectrosity but i can not assign a collider to the Game Object Vector Drawnline. Is there any chance to assign a collider to the lines or anybody now how can i do that??

Thank´s

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Pilchardo · Aug 23, 2014 at 10:02 PM

You can add a collider to a line by setting the 'collider' property to 'true'.

 // Create the line's points.
 Vector2 lineStart = new Vector2(10, 10);
 Vector2 lineEnd = new Vector2(20, 20);

 // The line's width.
 int width = 5;

 // Create the line object.
 VectorLine line = new VectorLine("SuperLine", new Vector2[] { lineStart, lineEnd }, null, width);

 // Add a collider.
 line.collider = true;

 // Draw the line
 line.Draw();

When drawing the next part of the line, you can detect collisions with the existing part by using 'Physics2D.Raycast'.

 // Create the 2nd line's points.
 Vector2 line2Start = new Vector2(0, 0);
 Vector2 line2End = new Vector2(45, 26);
 
 // Calculate the new line's direction.
 Vector2 direction = line2End - line2Start;
 direction.Normalize();
 
 // Raycast along the line to see if it hits anything.
 RaycastHit2D hit = Physics2D.Raycast(line2Start, direction);
 
 if (hit.collider == null) {
    // Didn't hit anything so draw the line.
    VectorLine line2 = new VectorLine("MegaLine", new Vector2[] { line2Start, line2End }, null, width);
    line2.Draw();
 }

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 Tomer-Barkan · Nov 20, 2013 at 04:44 PM

Instead of using lines, you can use cubes with very small scale in x & z axis, so they appear like lines but then a box collider will work.

Create a prefab of a cube, leave the scale 1,1,1, and add a box collider. Then when you want to draw a line from point A to point B you would use the following method (give it the prefab of the cube, the start and end position, and the width you want the line to have):

 public void DrawLine(Transform linePrefab, Vector3 from, Vector3 to, float lineWidth) {
     framesWithoutFiring = 0;

     // calculate vector from point A to point B
     Vector3 lineVector = to - from;
     
     // instantiate line
     Transform lineInstance = GameObject.Instantiate(linePrefab);
     
     // set position of line instance to center of line
     lineInstance.position = from + (lineVector / 2f);
     
     // set rotation of line instance so that it's Y axis is from point A to point B
     lineInstance.transform.up = toTarget.normalized;
     
     // set length of line instance
     lineInstance.transform.localScale = new Vector3(lineWidth, lineVector.magnitude, lineWidth);
 }
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

19 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

Related Questions

Internal collisions 1 Answer

Adding a Mesh Collider to Line Render in AR Draw 1 Answer

How can I make my players change colors when they collide? 2 Answers

spawn a ball in a football game 0 Answers

isTouching() with multiple colliders 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