Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Colliders on Prefabs only having

Discussion in 'Scripting' started by awollocko, Oct 30, 2015.

  1. awollocko

    awollocko

    Joined:
    Feb 17, 2015
    Posts:
    13
    Hey everyone,

    So i'm having a slight issue with instantiating a "link" prefab on a typical node-link style network visualization app i'm creating. Basically, I have the user create different nodes, and they can create links between them. The links are instantiated as a prefab, and the drawing is done as a LineRenderer. (See first attachment)

    My issue here is that I additionally have attached a cylinder collider to this object, because that was simplest to follow the path of the drawn/rendered line. However, the issue i'm having is that the line gets fairly large, and it seems the Cylinder Collider is empty. What i mean by that is that my typical Raycast hit detection to determine if a user has clicked on the link is ONLY triggered on the bounds/edges of the collider. (See attachment2 == it only trigggers if they click within the green collider bounds). I want it to detect a collision no matter where on the line (within the collider) the user clicks. Seems like it should be pretty straightforward....

    Is there any way to make a collider solid?
    Code (CSharp):
    1. lineRenderer = gameObject.AddComponent<LineRenderer>();
    2.         capsule = gameObject.AddComponent<CapsuleCollider> ();
    3.  
    4.         capsule.radius = (lineWidth / 2) + .1f; // slightly bigger click.
    5.         capsule.center = Vector3.zero;
    6.         capsule.direction = 2; // Z-axis for easier "LookAt" orientation

    Thanks,
     

    Attached Files:

  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hello,
    Is this 2D or 3D?
     
  3. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Have you verified inside the editor that the capsule you are creating is in fact covering the entire line? You will prrobably need to show a little more of your setup and code. A capsule normally picks up raycasts fine.
     
  4. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    As far as I understand the initial post his problem is that the line don't has an unique width. The line starts with a small width and ends with a bigger width (or vice versa). Due to this he can't cover the line using a standard capsule, box, etc. collider.
    And since there is no such thing like a "line collider" I guess he'll have to manually calculate the collision, or use a polygon or mesh collider or something. But all this (imho) depends on the type of the project (3/2D).
     
  5. Wallawocko

    Wallawocko

    Joined:
    Nov 1, 2015
    Posts:
    25
    Its a 2D project. Not sure why my username changed, but i'm OP. Am i maybe just doing something wrong? This is the first time i've used a 2D project, and capsule colliders.

    @ThermalFusion - The second attached screenshot highlights the collider. Its clearly there, but only collides on the outside bounds of the capsule.
     
  6. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I missed that in the second shot, sorry.
    You say you are making a 2D project yet you are using 3D colliders. You need to show us your implementation of your raycasting and describe what you want to achieve. Since you're using 3D colliders and you can hit some parts of the collider it seems to me as if maybe you start your ray in a position that might possibly be inside the capsule.
    If your project is 2D you should consider using 2D colliders and Physics2D instead, two Circle and a Box collider to make a capsule.