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 deneme09 · Aug 26, 2018 at 03:56 PM · colliderlinerendererclick

Detect mouse click on line renderer

Hello guys! I have a problem and I need help.

There is a line renderer in my scene. I have to make this clickable but I can't because it hasn't a collider and rigidbody. How can I make this, clickable?

You know, it's a line renderer so I can't add a collider to.

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 eses · Aug 26, 2018 at 04:39 PM 0
Share

@deneme09 - well googling brings this result as first - probably many ways to do things but this is one: https://answers.unity.com/questions/470943/collider-for-line-renderer.html

avatar image fdz_ · Aug 26, 2018 at 06:21 PM 2
Share

What about using a $$anonymous$$esh Collider? That would mean generating a mesh based on the Line Renderer. $$anonymous$$aybe you could use this LineRenderer.Bake$$anonymous$$esh.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by deneme09 · Aug 27, 2018 at 12:56 PM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(LineRenderer))]
 public class LineCollider : MonoBehaviour
 {
     LineRenderer line;
     public void Start() {
         AddCollider(6); //Increase the count of parts(6) if you want to get more detailed collider
     }
     public void AddCollider(int part)
     {
         try
         {
             line = GetComponent<LineRenderer>();
             var start = line.GetPosition(0);
             var end = line.GetPosition(line.positionCount - 1);
             var a = (line.positionCount - 1) / part;
             for (int i = 1; i<=part; i++)
             {
                 if (i == 1)
                     AddColliderToLine(start, line.GetPosition(Mathf.CeilToInt(a * 1)));
                 else if (i == part)
                     AddColliderToLine(line.GetPosition(Mathf.CeilToInt(a * (i - 1))), end);
                 else
                     AddColliderToLine(line.GetPosition(Mathf.CeilToInt(a * (i - 1))), line.GetPosition(Mathf.CeilToInt(a * i)));
             }            
         }
         catch
         {
             Destroy(gameObject);
         }
     }
 
     private void AddColliderToLine(Vector3 start, Vector3 end)
     {
         var startPos = start;
         var endPos = end;
         BoxCollider col = new GameObject("Collider").AddComponent<BoxCollider>();
         col.transform.parent = line.transform;
         float lineLength = Vector3.Distance(startPos, endPos);
         col.size = new Vector3(lineLength, 0.175f, 0.25f);
         Vector3 midPoint = (startPos + endPos) / 2;
         col.transform.position = midPoint;
         float angle = (Mathf.Abs(startPos.y - endPos.y) / Mathf.Abs(startPos.x - endPos.x));
         if ((startPos.y < endPos.y && startPos.x > endPos.x) || (endPos.y < startPos.y && endPos.x > startPos.x))
         {
             angle *= -1;
         }
         angle = Mathf.Rad2Deg * Mathf.Atan(angle);
         col.transform.Rotate(0, 0, angle);
     }
 }

I fixed with this :)

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 TheCircadian · Nov 01, 2020 at 05:59 AM 0
Share

When I try that I get this error. alt text

Also, why not just try AddCollider(GetComponent<LineRenderer>().positionCount) so it always matches the number of points in the line? (It didn't work for me, same error, so I'm imagining there's a reason)

error.png (94.6 kB)

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

112 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Click to Move rotation + collider issue 0 Answers

Determining which collider attached to same object was clicked 2 Answers

LineRenderer & EdgeCollider problem 0 Answers

How to toggle off / on raycast on a Gameobject 1 Answer

Problem with OnMouseDown(not registering clicks in some situations) 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