Search Unity

Unity Linecast not detecting colliders

Discussion in 'Scripting' started by suctioncup, Aug 22, 2012.

Thread Status:
Not open for further replies.
  1. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Code (csharp):
    1. #pragma strict
    2.  
    3. var cube2 : Transform;
    4.  
    5. function Update () {
    6.    
    7.     Debug.DrawLine(transform.position, cube2.position, Color.black);
    8.  
    9.     if (!Physics.Linecast (transform.position, cube2.position))
    10.     {
    11.         Debug.Log("Collider");
    12.     } else {
    13.         Debug.Log("No Collider");
    14.     }
    15. }


    From what I can see, my code is correct. However, I never get the log 'Collider', just repeating 'No Collider'. All cubes are default, but with different names, and thus they all have a box collider.

    Help?
     
  2. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Have you tried removing the '!' before Physics.Linecast?
    From what I'm seeing, it prints No Collider when a collider is colliding it. So yes, it is correct.
     
  3. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Wow, can't believe I made a mistake that elementary. Thanks.

    EDIT: I did that, and now it just prints 'Collider', even if there is nothing between it.

    DOUBLE EDIT: I figured out that the line cast (From cube1) was detecting the box collider on cube2. Removing this collider makes everything fine, but I need this code to work in a game, and in the game both characters have colliders. Help?
     
    Last edited: Aug 22, 2012
  4. jcomouth_de

    jcomouth_de

    Joined:
    Dec 28, 2017
    Posts:
    5
    Just a little reminder for everyone falling into the same problem: if a game object is marked as static, it will not be detected by line cast. It's like it does not exist.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Please don't necro threads. Especially when what you're posting isn't correct. The Static flag has no impact on physics queries.
     
Thread Status:
Not open for further replies.