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

Debug.DrawLine won't "stick" with Camera bottom-left corner, shifts with character movement

Discussion in 'Scripting' started by tytbone, Aug 4, 2015.

  1. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    For some reason Debug.DrawLine won't stay with my camera's bottom-left corner (origin.x), and instead moves slightly left or slightly right when the camera shifts. I assume the issue has something to do with converting to ScreenToWorldPoint but I'm not smart enough to figure out what's going on on my own.

    Here's my code (the line gets drawn from an enemy's collider2D min.x to the Camera's origin.x, which is suppose to be the Camera's "min.x" in theory):

    Code (csharp):
    1. Vector3 origin = Camera.main.ScreenToWorldPoint(Camera.main.transform.position);
    2.  
    3.         Debug.DrawLine(new Vector3(transform.collider2D.bounds.min.x, transform.collider2D.bounds.min.y), new Vector3(origin.x, origin.y));
    4.  
    5.         if (transform.collider2D.bounds.min.x < origin.x)
    6.             Debug.Log(transform.collider2D.bounds.min.x + ", " + origin.x);
    Image showing the difference, if it helps: http://i.imgur.com/4VC8kku.png
     
    Last edited: Aug 4, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    [code ][/code ] tags rather than list :p (sticky at the top of the scripting forum :) it's a little hidden away in the edit function bar, or just type them manually :rolleyes:)
     
    tytbone likes this.
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    You were correct in your assumption! The first line is the problem. You're trying to convert the cameras transform position from screen space to world space. It's already in world space, so the conversion is unnecessary.

    Also, I'm assuming you're using a version of unity earlier than 5, because the collider2D quick-reference variable is gone.

    Code (CSharp):
    1. public class Test : MonoBehaviour {
    2.    
    3.     Collider2D coll;
    4.  
    5.     void Start() {
    6.         coll = GetComponent<Collider2D> ();
    7.     }
    8.  
    9.     void Update() {
    10.  
    11.         Bounds bounds = coll.bounds;
    12.         Vector3 origin = Camera.main.transform.position;
    13.  
    14.         //no need to create new Vector3's from scratch. They already are Vector3s!
    15.         Debug.DrawLine (bounds.min, origin);
    16.  
    17.         if (bounds.min.x < origin.x)
    18.             Debug.Log(bounds.min.x + ", " + origin.x);
    19.     }
    20.    
    21. }
     
  4. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    Thanks Ben, that gets the line to the center of the camera, but unfortunately I can't figure out how to get the line (what math is needed) to attach to the corner bounds of the viewport (lower-left corner, upper-right-corner, etc.). I was thinking about adding a Collider2D to the camera (as a Trigger), but I keep thinking there's a better way to handle this.

    I thought maybe subtracting the orthographicSize might work since that's half the camera, but no dice.

    Code (CSharp):
    1. Debug.DrawLine(transform.collider2D.bounds.min, new Vector3(origin.x - Camera.main.orthographicSize, origin.y), Color.green);
     
  5. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    Okay, I think I kind of solved the problem thanks to http://blog.projectmw.net/unitys-screen-point-viewport-point-and-world-point and this code:

    Code (csharp):
    1. Vector3 fullscreen = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width-Screen.width, Screen.height-Screen.height));
    2.  
    3. Debug.DrawLine(transform.collider2D.bounds.min, new Vector3(fullscreen.x, fullscreen.y), Color.red);
    This gets the line to the bottom left of the viewport and keeps it there. Now I need to figure out how to compare it to enemy bounds.
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    (I was writing this as you posted your reply)
    THIS is where you would use ScreenToWorldPoint, or ViewportToWorldPoint.

    Bottom left would be 0,0.
    For the viewport, topRight would be 1,1.
    For ScreenPoint, topRight would be ScreenWidth,ScreenHeight.

    Since viewport is slightly less code, let's use that. Handily enough, we can use either a Vector2 or a Vector3 for this purpose (because implicit conversions exist), and there are already shorthand methods for each:

    Code (CSharp):
    1. Vector3 bottomLeft   = Camera.main.ViewportToWorldPoint (Vector2.zero);
    2. Vector3 bottomRight  = Camera.main.ViewportToWorldPoint (Vector2.right);
    3. Vector3 topLeft      = Camera.main.ViewportToWorldPoint (Vector2.up);
    4. Vector3 topRight     = Camera.main.ViewportToWorldPoint (Vector2.one);
    5.  
    6. Debug.DrawLine (bounds.min, topRight);
    7.  
     
    tytbone likes this.
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Haha, you figured it out as I was posting, awesome!

    I should point out, however, that Screen.width - Screen.width is just 0.

    You could make a helper method, if you want to easily switch corners:

    Code (CSharp):
    1.     Vector2 ViewportCorner(bool top, bool right)
    2.     {
    3.         float x = right ? 1f : 0f;
    4.         float y = top ? 1f : 0f;
    5.  
    6.         return Camera.main.ViewportToScreenPoint(new Vector2(x,y));
    7.     }
    Code (CSharp):
    1. //bottom right
    2. Debug.DrawLine(collider.bounds.min, ViewportCorner(false, true));
     
    Last edited: Aug 4, 2015
    tytbone likes this.
  8. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    Thanks Ben, that worked! Really appreciate your help.
     
    BenZed likes this.