So to me it seems like this code should do a Raycast, and alert me when it hits the player. It should also draw a DebugLine so I can see it. But neither of those things are happening. Can anyone see what I'm missing? Code (CSharp): public void Alert() { if(!alerted) { RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector3.right, searchDist * -1, collisionMask); Debug.DrawLine(transform.position, new Vector3(transform.position.x + searchDist, transform.position.y, transform.position.z)); if(hit.collider != null) { if(hit.collider.tag == "Player" && playerLoco.hidden == false) { Debug.Log("Ray has hit: " + hit.collider.name); } } } }
If the code is ran every frame then the line should be visible, but otherwise you wont see it (e.g. if it's called from Update, you should see a line). Also just an improvement suggestion, to aid code readability and to ensure your line is being drawn correctly: Code (CSharp): public void Alert() { if(!alerted) { Ray ray = new Ray(transform.position, Vector3.right); RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, searchDist * -1, collisionMask); Debug.DrawLine(ray.origin, ray.origin + (ray.direction * (searchDist * -1))); if(hit.collider != null) { if(hit.collider.tag == "Player" && playerLoco.hidden == false) { Debug.Log("Ray has hit: " + hit.collider.name); } } } } Ray is 3D but can store the details of your origin and direction for your raycast, makes it easier to ensure your line is being drawn correctly. Hope this helps!
You'd want to use Debug.DrawRay if you're doing to debug rays. You can use DrawLine but it's simpler just to use DrawRay. --Eric