• 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
Question by siddharth3322 · May 16, 2014 at 09:46 AM · collisionlinerendererlineunity4.3

Line Collision Detection for 2D Game

I am developing game related to line drawing in 2d.

At present I can able to draw line using Line Renderer. But I want to detect its collision also but right now I can't able to find any way.

I also used Google for same t$$anonymous$$ng but in that I don't found any useful content related to t$$anonymous$$s.

Comment
mamap

People who like this

1 Show 8
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 Noob_Vulcan · May 16, 2014 at 09:57 AM 0
Share

use raycast for that....will wrk fine

avatar image siddharth3322 · May 16, 2014 at 10:04 AM 0
Share

I don't have any idea related to ray casting in line collision detection. Any way thanks for your suggestion.

avatar image Noob_Vulcan · May 16, 2014 at 10:28 AM 0
Share

use raycast to detect collison and then draw your line upto that,.....

avatar image siddharth3322 · May 16, 2014 at 10:39 AM 0
Share

For each segment of line, I have to attach which type of collider?

avatar image MikeNewall · May 16, 2014 at 04:43 PM 1
Share

If you don't want to go down the maths route I have some code from a previous project which creates a collider between two points of a line renderer. If you had a collider on each line you could cast a ray from each line to see if it hit any others:

 public float lineWidth = 0.5f;

 private GameObject pointB;
 private LineRenderer lRenderer;
 private BoxCollider col;
 
 void Start(){

     if(!(lRenderer = GetComponent<LineRenderer>())){
         lRenderer = gameObject.AddComponent<LineRenderer>();
     }

     // Setup line renderer
     lRenderer.SetVertexCount(2);
     lRenderer.SetWidth(lineWidth, lineWidth);

     // Create point B
     pointB = new GameObject("Point B");
     //pointB.transform.parent = transform;
     pointB.transform.position = Vector3.zero + transform.right * 10;

     col = gameObject.AddComponent<BoxCollider>();

 }

 void Update(){
     // Update line renderer positions
     lRenderer.SetPosition(0, transform.position);
     lRenderer.SetPosition(1, pointB.transform.position);

     // make point A (our position) look at point B
     Vector3 direction = pointB.transform.position - transform.position;
     transform.right = direction.normalized;

     // Resize collider to fit line renderer
     col.center = Vector3.right * direction.magnitude /2;
     col.size = new Vector3(direction.magnitude, 1,1);
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by amila_15 · Jan 17, 2018 at 02:00 PM

Hello @ siddharth3322 , I search a solution for my own problem. By chance I was directed to your problem. T$$anonymous$$s tutorial will definitely help you. Visit https://www.youtube.com/watch?v=dmBQQ2XtuhU∈dex=72&list=LLYbK_tjZ2OrIZFBvU6CCMiA . If t$$anonymous$$s what you wanted, make t$$anonymous$$s as the answer. It will help others.

Comment
siddharth3322

People who like this

1 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 siddharth3322 · Jan 17, 2018 at 04:42 PM 0
Share

@amila thanks for reply but this is really old question, you have answered :) But thank you other users definitely get some outcome from this.

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

23 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

Related Questions

How to check of a line renderer collides with ground ? 1 Answer

Trigger single collision event 2 Answers

At same positioned game objects collision detection 0 Answers

LineRenderer. Problem with ray displaying 2 Answers

Create a collider for area created by 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