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!
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!
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!
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.
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
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
Hi, Im in the middle of converting my PC game to Xbox. I've got the mouse pointer moving with the Xbox controller sorted. BUT I've got a level selector which did use buttons on PC , but I can't get the raycast to work for the canvas, have you any ideas or do a quick tutorial? Thanks
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.
So box cast, capsule cast, and sphere cast is basically just like the Raycast but it’s just a shape at the tip depending on what type of cast you are using, right?
😉 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
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
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
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?
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!
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 ...
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.
@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.
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 I'm always impressed and appreciative when a youtuber replies to their viewers, and also so quickly :) Looking forward to this quick video!
@@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 😊
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?
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
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
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?
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
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?
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.