Search Unity

Bug RaycastHit2D Not Working

Discussion in 'Scripting' started by Ch267, Jan 23, 2021.

  1. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    I'm using a ray as a line of sight for one of the bosses in my game, but the ray won't work at all.

    Here's the script holding the ray:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class V2GuardLOS : MonoBehaviour
    6. {
    7.     public float distance;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         RaycastHit2D lineOfSight = Physics2D.Raycast(transform.position, transform.right , distance); // make a ray to detect if our player has entere the boss room
    13.         if (lineOfSight.collider != null) // if our ray hits something...
    14.         {
    15.             Debug.DrawLine(transform.position, lineOfSight.point, Color.red); // draw a line in scene view
    16.         }
    17.         else
    18.         {
    19.             Debug.DrawLine(transform.position, transform.position + transform.right * distance, Color.green);
    20.         }
    21.     }
    22. }
    23.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Debug.DrawLine
    will only be shown in the game-view window when Gizmos are enabled.
    Be sure to enable Gizmos on the game-view window if you haven't. upload_2021-1-22_21-54-8.png
     
    Madgvox likes this.
  3. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    I tried that and still nothing. Am I doing anything else wrong?
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Check the following:
    • Did you add the script to a GameObject in the scene?
    • Is the GameObject enabled?
    • Is the script enabled?
    • Is the "distance" field greater than 0? (Is it a large enough number in general to be seen?)
     
  5. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    I've put the script on the GameObject I want to shoot the ray from, I checked if the GameObject and script are enabled, and the distance is 50, as shown here:
    Is it that I have the GameObject prefabbed?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Make an empty scene, add an empty game object, add this script, turn it sideways so you can see the ray, press play.

    If that doesn't work, stop there and figure out why. See above. Strip it down, just do ONLY the Debug.DrawLine()... In fact, I just did this with your unmodified script above, put it on a blank GameObject at (0,0,0) rotated (0,45,0 and it works:

    Screen Shot 2021-01-27 at 11.11.03 AM.png

    So we know it works, strip EVERYTHING else out, PROVE that what you think is failing (the drawline) is actually failing. I don't think it is. Isolate, delete, isolate, remove, isolate... basic engineering 101.
     
    Ch267 likes this.
  7. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Thank you for this! Using this method, I've found that the game object that this script is for is causing problems. The ray itself is working 100% as expected.
    If anyone would like this for reference, here's the solution I found with this method:
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         Physics2D.queriesStartInColliders = false; // the ray no longer detects it's own collider
    4.     }
     
    Last edited: Jan 28, 2021