Raycasts, Explained. Every Physics "Cast" Visualized | Unity Tutorial

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 18. 06. 2024
  • Many of us kind of jumped into gamedev without a solid understanding of these Physics APIs such as Raycast and Spherecast. In this video I aim to make it really clear how each of the Ray, Sphere, Box, and Capsule casts work, look, behave, and how you can use each one of them to achieve your "casting" goals!
    In this tutorial video you can see the following "casts" visualized:
    1. Raycast
    2. RaycastAll
    3. Spherecast
    4. SpherecastAll
    5. Boxcast
    6. BoxcastAll
    7. Capsulecast
    8. CapsulecastAll
    each with different variations with trigger collision, layer masking, and more!
    With 19 customizable scenarios, you know the full project being available on GitHub (github.com/llamacademy/raycas...) will help you better understand how to use each of these fundamental gamedev functions!
    💾 Ongoing sales 💾
    ⚫ See all active asset sales on the Asset Store: assetstore.unity.com/?on_sale...
    ⚫ Save 25% off your first Asset Store Order: prf.hn/click/camref:1101l9QvC...
    ⚫ Save up to 50% on NEW Assets: assetstore.unity.com/?new_sal...
    📚 Resources
    ⚫ Docs page: docs.unity3d.com/ScriptRefere...
    ❀ Believe in LlamAcademy's mission and have received value from the videos? Become a Patreon Supporter or CZcams Member:
    ⚫ Patreon: / llamacademy
    ⚫ CZcams Member: / @llamacademy or click the Join button on any video
    đŸ“± Let's stay connected
    CZcams: / llamacademy
    Patreon: / llamacademy
    Facebook: / llamacademyofficial
    Twitter: / thellamacademy
    TikTok: / llamacademy
    Instagram: / llamacademy
    ----
    Most tutorials come from knowledge gained making survival.llama.software Llama Survival - a top-down zombie survival shooter for Android and iOS.
    I also have some Unity Assets (affiliate link): assetstore.unity.com/publishe...
    Some links may be affiliate links, which at no additional cost to you, gives me a small portion of the purchase.
    #unitytutorial #tutorialtuesday #gamedev #tutorial #unity #llamacademy #gamedevelopment
    Chapters:
    00:00 What are Raycasts?
    03:19 Raycasts Visualized
    05:00 Raycast All Visualized
    06:48 Ultimate Fantasy Unity RPG Developer Bundle!
    08:23 Spherecast Visualized
    09:02 Boxcast Visualized
    09:53 Capsulecast Visualized
    11:15 SpherecastAll Visualized
    12:37 BoxcastAll Visualized
    13:08 CapsulecastAll Visualized
    14:19 The NonAlloc-Variants

Komentáƙe • 84

  • @leonard4
    @leonard4 Pƙed rokem +6

    Just found your video, been fighting a Dash move where I was seeing players dashing through fences and through small gaps sometimes due to a bad raycast setup. Switched to a BoxCast and its all working now, never knew that existed, super useful video!

  • @bgmain525
    @bgmain525 Pƙed 6 měsĂ­ci +2

    This video deserves more views. I feel like there is no reason to watch any other raycast video!
    I'm very appreciative for you taking the time to document this so thoroughly!

  • @puntalic
    @puntalic Pƙed rokem +3

    Specially love the last part.
    Know your tools helps you build better and faster. Thanks

  • @a3dadventure79
    @a3dadventure79 Pƙed 4 měsĂ­ci +1

    thanks so much, really appreciated the depth as well as the time you took to learn and demonstrate in scene. keep up the great academy work!

  • @victorh.fagundessantos6774
    @victorh.fagundessantos6774 Pƙed rokem +7

    wow, that was very educative and helpful. Great explanation keep it going!

  • @durvids474
    @durvids474 Pƙed rokem +1

    Great stuff as always! Thank you for the video!

  • @AlexBlackfrost
    @AlexBlackfrost Pƙed rokem +1

    That was a very good explanation, keep up the good work!

  • @mattsponholz8350
    @mattsponholz8350 Pƙed rokem +1

    Thank you for this excellent resource and your encouragement/positivity!

  • @thepolygonpilgrimage
    @thepolygonpilgrimage Pƙed rokem +3

    Beautiful explanation and demo! Keep up the great work!

  • @neoteraflare
    @neoteraflare Pƙed 8 měsĂ­ci +1

    Wow this was a really high quality explanation!

  • @klausbdl
    @klausbdl Pƙed rokem

    thank you, you helped me understand the nonalloc variants and how to use it

  • @RootLocusSquare
    @RootLocusSquare Pƙed rokem +2

    much appreciated for the visualization !

  • @lim9527
    @lim9527 Pƙed rokem +1

    This is exactly what I was wondering, thank you so much

  • @min-of8cv
    @min-of8cv Pƙed 7 měsĂ­ci +1

    this is the best unity's raycast visualization on youtube !

  • @levayv
    @levayv Pƙed 9 měsĂ­ci +2

    WOW nice effort to show all this Thanks

  • @regys9521
    @regys9521 Pƙed 7 měsĂ­ci +1

    So cool! thank you very much

  • @astralpunchstudios
    @astralpunchstudios Pƙed 4 měsĂ­ci

    Best raycast tutorial in YT.

  • @wanlliifpsgamedev
    @wanlliifpsgamedev Pƙed rokem +2

    Man, you really are a teaching god. You give every explanation and every possible scenario. I really hope you can continue to help us achieve our dreams. I'll particularly try to help you on patreon as quickly as I can, even if it's on the simplest levels. And when I have a better condition, I will be at the highest levels to be able to help you as much as possible.

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem

      Wow! I really appreciate that! You are awesome đŸ€—

    • @AaronAsherRandall
      @AaronAsherRandall Pƙed rokem

      I agree this is really amazing. Thank you so much!

  • @Pos44Dami
    @Pos44Dami Pƙed rokem +1

    how can you have some dislike ?? you did a great job and you give the project !! damn +1

  • @rsplayz7777
    @rsplayz7777 Pƙed 9 měsĂ­ci +1

    Damn, I finally understand spherecast, I was thinking like it only casts sphere to end point but seeing it animated fixed it for me. It has no difference than Raycast, it was my mistake to think otherwise!

  • @jcd302
    @jcd302 Pƙed rokem +1

    Wow this is great đŸ‘đŸŒ

  • @518wolf
    @518wolf Pƙed 4 měsĂ­ci

    Thank you you so much, the visuals are awesome!!! I'm curious if you have a video on the frame generations of this?Are these generated every frame or in relation to rendering in some form

  • @heman922
    @heman922 Pƙed rokem +1

    Plz do more videos about using raycast and job system and rigidbody calculations with job system ❀❀❀❀

  • @andrewallbright658
    @andrewallbright658 Pƙed rokem +2

    This is pretty helpful information for me (yeeesss me). Jokes aside I never knew about the QueryTriggerInteraction enum. Neat!

  • @Golden_games
    @Golden_games Pƙed rokem +4

    love your intro. that never gets old for me.

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem +2

      â˜ș thanks. It's one of my favorite parts

  • @SeanBotha
    @SeanBotha Pƙed rokem +1

    really helpful thank you

  • @momomaster5751
    @momomaster5751 Pƙed rokem +1

    Like to see more unity physics and job system videos 👍

  • @okunamayanad
    @okunamayanad Pƙed rokem +1

    How did you make those casts slower than usual?
    Did you code them again by checking collisions?

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem

      😉 the cast itself still only happens in 1 frame. I have just shown a visualization slowly to make it more understandable. You can see exactly how I did it if you check out the project on GitHub: github.com/llamacademy/raycasting-explained

  • @ManyMagix
    @ManyMagix Pƙed 2 měsĂ­ci

    Great video! Do you think a boxcast might be a good way to deal with super fast sword slices instead of relying on Unity's collision detection? I'm thinking I might boxcast a weapon's box collider a set distance forward for every Update cycle while the weapon is in an "attack is active" mode. If it hits a player or something "hittable" then we simulate a collision. I've been messing with the different collision detection modes Unity offers on the rigid body but none have really gotten me quite where I want ...

    • @LlamAcademy
      @LlamAcademy  Pƙed 2 měsĂ­ci

      It could work, but most likely your slash doesn't move perfectly linearly (but instead, more likely spherically) which can result in misses when your visual representation would have hit. For a sword slash I think you want "Continuous Dynamic" on a Rigidbody that is moving to get the proper collision.

    • @ManyMagix
      @ManyMagix Pƙed 2 měsĂ­ci

      @LlamAcademy thanks for the reply! I've tried dynamic and speculative and both work 9 out of 10 times ... but I want 10/10. I'm thinking I fire a rectangle shaped ray the width and length of the blade each frame and see what kind of precision is possible. I don't want to have to slow down the attack speed it seems less realistic 😅. Love your vids btw.

  • @ItsMidNightDev
    @ItsMidNightDev Pƙed 3 měsĂ­ci

    Hi, super informative tutorial!
    If I want to check if 2 specific objects have been hit by RaycastAll, and I only want to do something if both objects have been hit, not just one, what would be the best way to track that?
    Sorry it's more of a programming question instead of a raycast question, but figured I'd ask on the off-chance of some suggestions/recommendations

    • @LlamAcademy
      @LlamAcademy  Pƙed 3 měsĂ­ci +1

      I think the most simple way would be to iterate over all the colliders hit by the RaycastAll/NonAlloc and check if both have been hit with a boolean like bool hitObject1, hitObject2; then assign them to true if the Raycast hit collider is the target object

    • @ItsMidNightDev
      @ItsMidNightDev Pƙed 3 měsĂ­ci

      @@LlamAcademy Thank you so much! :)

  • @Hyphen3372
    @Hyphen3372 Pƙed rokem +1

    awesome explanation man this is awesome! and i wanted to ask if you could make a tutorial for wall penetration or enemy body penetration. if you have time. thank you.

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem +1

      I have that on my list of topics! I plan on covering that as part of a gun & bullet series

    • @Hyphen3372
      @Hyphen3372 Pƙed rokem +1

      @@LlamAcademy Ok cool good to know and thanks for responding.

  • @Dippps
    @Dippps Pƙed rokem +1

    Theres also navmesh cast. Its kinda used when you want to know if agent see a spot

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem +1

      True! I was only thinking of physics casts, but the NavMesh cast is very similarly useful for checking if a NavMeshAgent will hit something when traversing on the NavMesh

  • @areallyboredindividual8766

    I'm working on a custom raycast implementation for a programming challenge and I am wondering whether, after creating a ray using ViewportPointToRay, would it be possible to check whether this ray has intersected with a mesh rather than with a collider?

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem +1

      I think that is feasible. It likely will be slow because you’ll have to check the bounds / triangle positions of each possible mesh and compare it outside the physics system. You’ll basically be rewriting a portion of the physics system

    • @areallyboredindividual8766
      @areallyboredindividual8766 Pƙed rokem

      @@LlamAcademy In my situation there is fortunately only one object, unfortunately it is a sphere and that could make this a lot more complex 😅

  • @HiddenExp
    @HiddenExp Pƙed rokem

    Hi! Can I ask Something? Is the Spherecast drawing a sphere with multiple raycasts all around?

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem

      Of course you can ask 🙂. That is not how the spherecast works though. Remember that a Ray is an incredibly thin line, so to construct a sphere with these would be extremely computationally expensive because we'd have to do thousands or millions of raycasts to ensure nothing was missed!
      You can think about it as if you were to actually take a sphere and move it along the path you are "casting" on and that will (hopefully!) eventually hit something

    • @HiddenExp
      @HiddenExp Pƙed rokem

      @@LlamAcademy Thanks for the answer 🙏

  • @TheRealLifeSonicTheHedgehog
    @TheRealLifeSonicTheHedgehog Pƙed 10 měsĂ­ci

    Trying to use the ray cast to allign a sphere to the surface of the ground and get the angle of the normal like sonic the hedgehog. Help please

    • @LlamAcademy
      @LlamAcademy  Pƙed 9 měsĂ­ci

      The RaycastHit has a "normal" property. Is that what you are looking for? docs.unity3d.com/ScriptReference/RaycastHit.html

    • @TheRealLifeSonicTheHedgehog
      @TheRealLifeSonicTheHedgehog Pƙed 9 měsĂ­ci

      @@LlamAcademy how can I keep the player above the surface of the ground? Or colliding with the ground. Keeping it aligned with the surface

  • @NoahSmith-School
    @NoahSmith-School Pƙed 8 měsĂ­ci

    I am a little confused on the sphere cast, im not sure how you get an output from it, Im really new to development and I am checking if something is above my head while i crouch, I had it working with a raycast but the cast was to thin to can the roof above the corner of my head, this would cause me to jump while coming out of a tunnel i was crouched in, the raycast just returned true or false, I tried using sphere cast in the exact same context but it wouldnt work, does anyone have the solution to this, am i just being stupid?

    • @LlamAcademy
      @LlamAcademy  Pƙed 8 měsĂ­ci

      You should be able to replace the Raycast exactly with a Spherecast but you have to remember to give the spherecast a radius as well. Everything else between them works the exact same way!

  • @jashum8554
    @jashum8554 Pƙed 9 měsĂ­ci

    Hope to adapt this in godot after the recent developments:(
    Thanks for the amazing tutorial!!!

    • @LlamAcademy
      @LlamAcademy  Pƙed 9 měsĂ­ci

      If I do switch to Godot
. I have a big backlog of videos to port over 😁 this will definitely be one of them!

  • @patrizioveglia5
    @patrizioveglia5 Pƙed 10 měsĂ­ci +1

    Danke!

    • @LlamAcademy
      @LlamAcademy  Pƙed 10 měsĂ­ci

      Wow! Thank you! 🧡 I appreciate that!

  • @mkbuhain
    @mkbuhain Pƙed rokem

    arrow forward once to get "Analog app 1 TE" like he uses in the video or simply find one you'd like to use alternatively instead.

  • @pacmaybz
    @pacmaybz Pƙed 2 měsĂ­ci

    cool video

  • @ty-xq7bl
    @ty-xq7bl Pƙed rokem +1

    you, yes you!

  • @ThanksForAllMyToes
    @ThanksForAllMyToes Pƙed rokem +1

    Please please make a video explaining raycasting on Canvas UI elements (floating world UI). I can't for the life of me get a hit on a canvas raw image, unless I put a 3D Box Collider on it, which seems so wrong and I don't want to do.

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem +1

      That is a great idea for a topic! Thank you 🙏

    • @ThanksForAllMyToes
      @ThanksForAllMyToes Pƙed rokem +2

      @@LlamAcademy I'm always impressed and appreciative when a youtuber replies to their viewers, and also so quickly :)
      Looking forward to this quick video!

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem +1

      @@ThanksForAllMyToes I try to respond to all of the questions! For an education channel I think it doesn't make a lot of sense to post content and not respond when someone has a question 😊

  • @dbweb.creative
    @dbweb.creative Pƙed rokem

    what is a sphere cast's step? I find it inconsistent depending on circumstances...

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem

      What do you mean by “step”?

    • @dbweb.creative
      @dbweb.creative Pƙed rokem

      well, from what I am understanding is that spherecast works under the hood the following way: a sphere test is being performed every X distance for the total distance specified in the specified direction. If that's correct, then what is the step distance between each sphere test?

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem

      I don’t think that’s correct. Each of these casts occurs in a single frame based on the current physics world. The sphere/box/capsule/ray moves continuously until it makes contact or runs out of distance. What you’re describing sounds more like if you were checking with some script via FixedUpdate, the “step” would be however far you move the object in between calls to fixed updated based on the physics update interval. For most things on Physics that have a step, Unity provides a configuration option for it here: docs.unity3d.com/ScriptReference/Physics.html

    • @dbweb.creative
      @dbweb.creative Pƙed rokem

      so what is the implementation behind a sphere cast? is it some kind of a mathematical formula that checks for interception? or is it doing many sphere tests along the line for a distance specified?

    • @LlamAcademy
      @LlamAcademy  Pƙed rokem

      I don’t know how Unity has implemented it. The only information they give is here: docs.unity3d.com/ScriptReference/Physics.SphereCast.html which does not mention implementation details.
      And really, the results are what we’re interested in. If we care deeply about how it’s implemented, that’s when we start going down the custom engine rabbit hole.

  • @abhishekbenny3340
    @abhishekbenny3340 Pƙed rokem

    damn its work

  • @karlkarl6176
    @karlkarl6176 Pƙed rokem

    Poor Ray, he gets so abused... casting and all...

  • @itep7982
    @itep7982 Pƙed rokem

    markiplier

  • @vegillito
    @vegillito Pƙed rokem +1

    where that GraphicRaycaster.Raycast at???? HMMMM ???