christopher snook (shocktr00p)

3 answers · asked · Lesson: Applying Force with a Raycast · Course: C# Bootcamp for Unity

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

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).
  • crew
    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.
  • 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.
  • crew
    Glad I could help!