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

Resolved Why does my raycast sometimes appear, and other times not?

Discussion in 'Scripting' started by Eversmann, Feb 28, 2023.

  1. Eversmann

    Eversmann

    Joined:
    May 3, 2013
    Posts:
    29
    If I set the players fingers to be the transform point of origin it does appear (albeit at the wrong angle). But if I add an empty game object as a child of my player and try to shoot the ray from there, nothing shows. Could it be scaling issue? The model I used has its rig at 100 scale. (I got it online somewhere so not sure how it was exported).

    I did try setting the ray length to like 500 but still nothing showed. Also tried setting so it excluded the player via layer cuz I thought it was hitting some collider.

    Any ideas?

    Edit: I am using Debug.DrawRay
     
    Last edited: Feb 28, 2023
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Raycasts don't 'show' unless you draw them yourself.
    Unity - Scripting API: Debug.DrawRay (unity3d.com)
     
  3. Eversmann

    Eversmann

    Joined:
    May 3, 2013
    Posts:
    29
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Are you sure you're actually calling the code? Is there an if-statement that could be failing anywhere, preventing you from raycasting in the first place?
     
  5. Eversmann

    Eversmann

    Joined:
    May 3, 2013
    Posts:
    29
    Quite positive. Cuz when I place it on the fingers, it does draw the line. When I place it anywhere else, I see no line. I will double check the code. But here is the code so you can see yourself.

    https://hatebin.com/hensoqpesn
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    The layermask on Physics.Raycast is for which layers to INCLUDE, not exclude.
    Could it be hitting the player itself?
     
    Bunny83 likes this.
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You're only drawing the ray when it hits something. So presumably it's not hitting anything when it's not being drawn.
     
    Eversmann and Bunny83 like this.
  8. Eversmann

    Eversmann

    Joined:
    May 3, 2013
    Posts:
    29
    You were correct indeed. Taking it out the brackets it showed just fine.
     
  9. Eversmann

    Eversmann

    Joined:
    May 3, 2013
    Posts:
    29
    This was not the case, the problem was I was drawing the ray only if it hit something.