I am slightly confused with the Raycast length and Hit. In my demo the Sphere is always hit when pressing the trigger. …

Forum Archive I am slightly confused with the Raycast length and Hit. In my demo the Sphere is always hit when pressing the trigger. …

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #4786
    christopher snook
    Participant

      I am slightly confused with the Raycast length and Hit. In my demo the Sphere is always hit when pressing the trigger. No matter how short I make the Ray Length. I have reviewed and copied the script from this lesson. I was under the impression that if would only register a hit if the Raycast touched the object (hit).

      #23074
      Jonathan Gonzalez
      Participant

        I should’ve mentioned it in the video, but the Ray Length in this case is just used to display the actual raycast in green. So the green ray is actually based off the length value, but the actual raycast is very very long. If you don’t specify a length within the Physics.Raycast section it’ll cast out a ray as far as it can, hence why it always hits the object.

        To fix this you’ll need to change it to something like this:

        Physics.Raycast (transform.position, fwd, out hit, rayLength))

        the last variable in this case being the length that you want to set for the raycast. If you have the rayLength variable within the DrawRay section it’ll also show you exactly how long it is since both sections are using the same variable for the length of the Raycast.

        #23075
        christopher snook
        Participant

          Jonathan

          Like always thank you for taking the time to answer my questions. This is a great site and I’m trying to soak up everything I can.

          #23076
          Jonathan Gonzalez
          Participant

            Glad I could help!

          Viewing 4 posts - 1 through 4 (of 4 total)
          • The topic ‘I am slightly confused with the Raycast length and Hit. In my demo the Sphere is always hit when pressing the trigger. …’ is closed to new replies.