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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Linecast and Debug.DrawLine Not Working

Discussion in 'Scripting' started by YBtheS, Aug 31, 2018.

  1. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    So I have this code to detect whether there is a game object with a collider between an NPC and the player. I tried to do this using a linecast but even when a game object with a collider is separating the two, the collider of the linecast returns null. I thought that the start and end position of the line may be the problem so I tried doing a Debug.DrawLine to test it and I don't see any line being rendered. Here's some of my code:

    Code (CSharp):
    1. void FixedUpdate() {
    2.             //Update AI and target position
    3.             target = player.transform.position;
    4.             pos = gameObject.transform.position;
    5.  
    6.             Attack();
    7.         }
    8.  
    9.         void Attack() {
    10.             lineHit = Physics2D.Linecast(pos, target, 1 << ~lineIgnoreLayer, -Mathf.Infinity, Mathf.Infinity);
    11.             Debug.DrawLine(pos, target, Color.red, 0f, false);
    12.             if(target.y > pos.y + 1 && lineHit.collider != null) {
    13.                 Debug.Log(lineHit.collider.gameObject);
    14.             }
    15.       }
    16. }
    Why does neither the Linecast nor DrawLine work methods work?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Maybe print out the Vector3.Distance() between target and pos, make sure they're not super-close together?
     
  3. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    I don't think that's the case because I control the player and when I test it out I run to the other side of the map.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Sure... but are those the values your code is seeing? :)
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,504
    Could it have something to do with your duration being 0, and/or this being in FixedUpdate()? Also, where are you looking for it to appear?
     
  6. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    I debugged the target and pos variables and the two are 20 units away on the x axis and 7 units away on the y axis.

    I changed it to both 10f and 100f and neither worked.

    Thanks for the replies though!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    By the way, did you turn on the Gizmos tab in the upper right corner of the Game window, right beside the "Stats" tab?
     
    a436t4ataf and angrypenguin like this.
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,504
    Are you looking in the Scene view?
     
  9. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Everything in the Gizmos tab is checked.

    Ahhh...I was looking in the Game view. I didn't know that I needed to be looking in the Scene view. Well that solves the DrawLine problem.

    Thanks so much guys! So does anyone know why my linecast might not work? The DrawLine shows that the start and end point are correct.
     
    angrypenguin likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Scene has a gizmos setting, and Game has a gizmos setting too... you can see Debug.DrawLine() in the Game window too, which is hugely helpful when playing your game in the editor.

    Just a few more things to test: do you have a 2D-style collider on the thing you're trying to hit? Physics2D.Linecast will absolutely NOT hit anything in the Physics system, only things in the Physics2D systems.
     
    angrypenguin likes this.
  11. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Oh I didn't realize that they were two separate things. Thanks I just enabled gizmos there and it works now.
    Yes. It has a Box Collider 2D component.
     
    angrypenguin likes this.
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Aha, I think there is a problem in your bit shift and bitwise invert.

    You have the term:

    1 << ~lineIgnoreLayer

    What that does is bitwise invert lineIgnoreLayer, then shift 1 left by that much.

    Try instead:

    ~(1 << lineIgnoreLayer)

    as your mask term. That will shift 1 left by the bit position of the layer you want to ignore, then bitwise-invert it so that it pays attention to all the other layers besides the one you're ignoring.
     
    the_real_ijed and YBtheS like this.
  13. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    That fixed it. Thanks a ton! That was really bothering me for a while.
     
    Kurt-Dekker likes this.