Raycasts in Unity (made easy)

  Рет қаралды 87,435

Game Dev Beginner

Game Dev Beginner

Күн бұрын

Want to know how to CODE in Unity? Go here: gamedevbeginner.com/how-to-co...
Learn how to use the Raycast function in Unity.
00:00 Intro
00:57 Creating a Ray
02:33 The Raycast Hit variable
03:11 The Raycast function
04:48 Layermasks
07:10 How to exclude Trigger Colliders
08:26 Raycast All
10:03 Raycast Non-Alloc
10:30 Other types of Raycast
13:32 Raycast in 2D
16:00 Outro
You'll learn how to create Rays, how to use Raycast Hit data from collisions when they happen and how all the different types of Raycast function work.
Read my full article here: gamedevbeginner.com/raycasts-...
Outro music: New Year by Bad Snacks
Unity icons created by Freepik - www.flaticon.com/free-icons/u...
Cursor icons created by Pixel perfect - www.flaticon.com/free-icons/c...
Chalkboard image - School rules photo created by kjpargeter: www.freepik.com/photos/school...

Пікірлер: 88
@MyPing0
@MyPing0 2 жыл бұрын
I've never used RayCasts before, so this video was great to learn something new. I do have a question though about what you say at 9:45. What do you mean by creating garbage? Why does it do that if it creates a new array? I'm assuming you are talking about it taking up memory, but I'm still not that deep into programming so I didn't quite understand that. If you could explain it that would be awesome! Great video as always :)
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Thanks! So I'm not an expert on garbage collection but my understanding of it is that some actions can create an amount of garbage, which is excess memory that won't be used again so, to prevent memory leaks, it's reclaimed by the garbage collector, an automatic program that runs in the background. And, when this happens, it can cause performance drops, stuttering the game. Functions that create gargage include making new objects, usually anything involving a string seems to do it (e.g debug.log) and creating new arrays, which is why, if you're using raycast a lot, every frame, you would probably want to use the non-allocating version instead of raycast all. Hope that helps! Let me know if you want to see a video on garbage collection in the future and I'll do a deep-dive on it.
@MyPing0
@MyPing0 2 жыл бұрын
​@@GameDevBeginner Oh I see. Your explanation is great, I actually didn't know about this at all! I would love a video that can explain this sort of stuff. It seems important to know about and yet this is the first of me hearing this lol Thanks again for the reply :)
@GameDevBeginner
@GameDevBeginner Жыл бұрын
My article on Garbage Collection in Unity is now live: gamedevbeginner.com/garbage-collection-in-unity-what-it-is-and-how-to-avoid-it/ I'll likely follow up with a video in the future.
@MyPing0
@MyPing0 Жыл бұрын
@@GameDevBeginner Wow dude thanks so much for the follow up! I'm gonna read this right now, you are so awesome!
@rishi76
@rishi76 6 күн бұрын
Can't believe it took KZfaq such a long time to recommend this channel to me!!!
@TheMellowed
@TheMellowed 3 күн бұрын
thank you, thank you, and once more; thank you. this video refreshing my memory on raycasts has actually just saved my entire Unity assignment (i'm good with creative stuff, not coding aha)
@martir1786
@martir1786 15 күн бұрын
PLEASE do a video like this on Angles and rotations. WE NEED IT SO BAD
@jatgreen2995
@jatgreen2995 Жыл бұрын
if only all tutorials were this good, concise and comprehensive with timestamps for whatever you're looking for. you've absolutely mastered tutorial creation
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thanks so much!
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 Жыл бұрын
this is spectacular. you're the best instructor in existence. Unity should pay you a million dollars to teach their software. finally a great coder who gets that other people dont know everything. thats the best instructor. you.
@notevenjuice1565
@notevenjuice1565 Жыл бұрын
Super underrated channel. Instruction was perfect thank you so much.
@MellioMusic
@MellioMusic Жыл бұрын
Your channel is starting to become my go-to for Unity/C# help. All your vids are so clear, high quality, no-BS. Thanks for all your great work!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thanks so much!
@techno_tuna
@techno_tuna Жыл бұрын
I had read how layer masks work, but your explanation was both cleaner, quicker, and more complete than anything I'd read till now. Thank you.
@zappist751
@zappist751 7 ай бұрын
The fact that this video is free to watch is unreal...
@mattmelbourne3688
@mattmelbourne3688 5 ай бұрын
People in this community generally want eachother to succeed
@JacqueCricket
@JacqueCricket 4 ай бұрын
Not Unreal, Unity
@Rovsau
@Rovsau Жыл бұрын
Out of all the videos I've seen on Raycast, this one really goes far and wide. Collider and Plane Raycast were new to me. Really good pacing, explanation and visualizations. Thanks for this great educational content!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@user-mn8pq9ky1u
@user-mn8pq9ky1u 9 ай бұрын
Thanks for explaining raycasts in unity from the start to the end, not excluding anything! I'm pretty sure you can't make *anything* without raycasts, so that was very helpful!
@MuhammadHosny0
@MuhammadHosny0 Жыл бұрын
Good job mentioning Non-Alloc it can be a life saver.
@neurotrollium253
@neurotrollium253 8 ай бұрын
Awesome, dude! I never have seen well explained how the binary list works until now, and for that alone you have my gratitude. Also, there is not a better explanation about the RayCast in Unity anywhere else. Your video is top notch and you have gained a new suscriber.
@kiel5390
@kiel5390 Жыл бұрын
IM SORRY WHAT! 660 views WHAT! This is better then 95% of the internet. Legit a slideshow and explanation. Insta like and sub GOOD JOB
@alpercizmeci4039
@alpercizmeci4039 Жыл бұрын
This is some god-tier tutorial. Thanks man keep it up!
@zaidmermam2524
@zaidmermam2524 2 жыл бұрын
thats awesome man! your explaination is awesome, the quality is awesome, the sound is crisp, the visuals are cool, great job!
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Thanks so much!
@simonvutov7575
@simonvutov7575 11 ай бұрын
Really good video man!! Been using unity for 6 years, but always need a reminder on these things every once in a while lol.
@333protoman
@333protoman Жыл бұрын
Outstanding explanation on raycasts
@amcmahon134
@amcmahon134 4 ай бұрын
This has helped me so much, thank you!
@user-lt4go4qb4b
@user-lt4go4qb4b 4 ай бұрын
A really great tutorial man, really helped me understand more about raycasts! subbed for more :)
@ampomahwinston3436
@ampomahwinston3436 Жыл бұрын
thanks a lot, timeline: 5:00 - 5:10, literally assisted me in an issue have been facing for 2 and half weeks.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Glad I could help!
@joemitchell7420
@joemitchell7420 Жыл бұрын
this is exactly (and i mean exactly) what i needed thank you so much :D
@thecopymaker2664
@thecopymaker2664 Жыл бұрын
Id used Raycasts in Unreal but never Unity, while being almost the same thing, I was struggling, this helped alot!!
@estam7264
@estam7264 2 жыл бұрын
Your videos are awesome man! Seriously the best unity tutorials on KZfaq (I just started using unity so take this with a bit of salt)
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Awesome, thank you!
@timothy_synth
@timothy_synth 10 ай бұрын
This video is fantastic, thank you 👍🏻
@Director414
@Director414 9 ай бұрын
Great video! SUper awesome, many thanks! :) Keep up the good work!
@nahuelhemsi
@nahuelhemsi 4 ай бұрын
This content is underrated
@user-yq9ro8hx5m
@user-yq9ro8hx5m Жыл бұрын
so clear 👌🏼,I like the way you explain
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@eltinhonunes
@eltinhonunes 3 ай бұрын
Great video, thanks, your didactic is amazing
@NanoGamingGamer
@NanoGamingGamer 3 ай бұрын
7:00 - you can also use 2^9 = 512. and if using multiplayer layers then just do 2^9 + 2^4 and you have your int.
@gctsquaternion6510
@gctsquaternion6510 2 ай бұрын
Amazing... thank you!
@otrqffaimajg
@otrqffaimajg 6 ай бұрын
Best explanation!
@patpatmo3171
@patpatmo3171 Жыл бұрын
nice tutorial, thanks
@EigenA
@EigenA Жыл бұрын
Great work
@MrZtapp
@MrZtapp 10 ай бұрын
Very good thanks
@olevareee3742
@olevareee3742 Жыл бұрын
This is an amazing video! I have been trying to get into coding and game development and this has been a lifesaver. It was super useful as it not only explains how to implement it, but also how it works, what does what, the different variants and even small things like Raycast all, non-alloc, and 2D/3D. Great job, I look forward to watching other videos on this channel in the future! also, I was wondering how I could calculate how far the collision occurs along the ray? Thanks!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you! Great to hear it helped you. Also, RaycastHit.distance is probably what you're looking for.
@olevareee3742
@olevareee3742 Жыл бұрын
@@GameDevBeginner Thank you so much!
@batnikelam-mavzer443
@batnikelam-mavzer443 2 жыл бұрын
Thanks 4 the video
@MarekNijaki
@MarekNijaki 2 жыл бұрын
Awesome video
@muhammadzubairirshad6724
@muhammadzubairirshad6724 Жыл бұрын
Just Amazing
@Hadys
@Hadys Жыл бұрын
good video thanks
@thebumblecrag61
@thebumblecrag61 Жыл бұрын
Fantastic 🎉
@TricoliciSerghei
@TricoliciSerghei 9 ай бұрын
The video is great thank you.. But I'd call it, Thorough Raycasts in Unity.
@furizagold
@furizagold Жыл бұрын
thx sis
@jaylini2250
@jaylini2250 Жыл бұрын
This video was actually very well explained. You deserve a lot more subcriptions. :) I have one additional question, how can i get the direction of a Raycast and turn it around. To use is as an force to push away object in the same angle they have been hit? (Selfknockback in a shooter)
@GameDevBeginner
@GameDevBeginner Жыл бұрын
So, to get the direction of a ray from the point of impact, towards the origin, use: (Ray.origin - RaycastHit.point).normalized
@jaylini2250
@jaylini2250 Жыл бұрын
@@GameDevBeginner Thank you very much!
@random_precision_software
@random_precision_software 3 күн бұрын
You left one out... The one i needed to know about...... 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
@random_precision_software
@random_precision_software 5 күн бұрын
I was hoping for answers for canvas raycast 😢
@macademia_p7853
@macademia_p7853 Жыл бұрын
Hi uh, i want to know, if you know about Steam VR player, it can be played without the VR and its going to use Raycast My question is, how to replicate those, i have strugle to implemented it so i can click the object that i aim in to, example the UI Canvas that trigered something if i press trigger on the VR that work with Mouse click as well.
@BorisAverin
@BorisAverin 8 ай бұрын
Супер
@GAMEBR0VIP2462
@GAMEBR0VIP2462 Жыл бұрын
how can i print the layer mask int that im looking at so i can find which one to ignore ?
@GameDevBeginner
@GameDevBeginner Жыл бұрын
I think you may be able to get the layer through RaycastHit > Collider > GameObject > layer
@NikELbErGErBergel
@NikELbErGErBergel 11 ай бұрын
This leaves the question open on what exactly a ray can hit. I have an empty gameobject that i want to be able to hit with ray what do I add to it so its hittable but does not get any other features like collision or extra visual things
@GameDevBeginner
@GameDevBeginner 11 ай бұрын
Add a collider, set it as a trigger and make sure that the raycast you use queries trigger interactions
@affenaffgaffel8330
@affenaffgaffel8330 Жыл бұрын
Hi, Great Video but I have one Question: how can I Calculate the direction from the Origin towards where the Raycast hit?
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Vector3 direction = (RaycastHit.point - Ray.origin).normalized; should work.
@affenaffgaffel8330
@affenaffgaffel8330 Жыл бұрын
Thanks it works perfectly now
@pulaksarkar7845
@pulaksarkar7845 Жыл бұрын
Hello Sir, I can’t able to add information by clicking different parts of a 3D object. Please help me , and if possible make a separate tutorial on that.
@xxxxx409
@xxxxx409 Жыл бұрын
colliders or raycast for 2d pixel art game ?
@brutosmomentos7355
@brutosmomentos7355 Жыл бұрын
Does anyone know the color theme he is using for the sytanx?
@GameDevBeginner
@GameDevBeginner Жыл бұрын
It's the Gruvbox theme in Visual Studio
@brutosmomentos7355
@brutosmomentos7355 Жыл бұрын
thx @@GameDevBeginner
@ChapCanai
@ChapCanai 8 ай бұрын
if this was easy then I don't think I wanna go advanced
@abg2487
@abg2487 2 жыл бұрын
How to make ping pong game in unity?
@jelleraffel5
@jelleraffel5 Жыл бұрын
Hey why is my raycast saying it hits the object but the raycast is not showing, not in the scene view and not in the game view. This is my used code ' Ray ray; public LayerMask layersToHit; void Start() { ray = new Ray(transform.position, transform.forward); Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue); CheckForColliders(); } void CheckForColliders() { if (Physics.Raycast(ray, out RaycastHit hit)) { Debug.Log(hit.collider.gameObject.name + " was hit "); Debug.DrawRay(ray.origin, ray.direction, Color.red, 10f); } }' Thanks!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
So your first drawn ray probably won't be visible because it only happens once in start and doesn't have a duration set. Your second ray should be visible for 10 seconds but will only be 1 unit long, so there's a chance you may not be close enough to see it? Otherwise it should work, I tried your code and it showed up as expected.
@janisyoutube
@janisyoutube Жыл бұрын
Just use .Any() instead of .Length > 0.
@Blurgert
@Blurgert 3 ай бұрын
I still don't get it
@tPlayerioT
@tPlayerioT Жыл бұрын
so annoying to find out that 2d and 3d are different while it took me 30min to find out what was happening that wasnt working
@GameDevBeginner
@GameDevBeginner Жыл бұрын
I do this all the time. When I can't work out why something just doesn't work, only to realise that I've used the wrong physics engine!
@tPlayerioT
@tPlayerioT Жыл бұрын
@@GameDevBeginner yeah 😆😅
Connecting scripts on the same object in Unity
15:36
Game Dev Beginner
Рет қаралды 10 М.
The right way to pause a game in Unity
9:09
Game Dev Beginner
Рет қаралды 57 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 58 МЛН
Каха заблудился в горах
00:57
К-Media
Рет қаралды 9 МЛН
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 24 МЛН
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 21 МЛН
Giving Personality to Procedural Animations using Math
15:30
t3ssel8r
Рет қаралды 2,5 МЛН
Unity 3D RayCast Collisions (In 2 Minutes!!)
2:19
Royal Skies
Рет қаралды 27 М.
How to jump in Unity (with or without physics)
16:09
Game Dev Beginner
Рет қаралды 43 М.
Unity Architecture for Noobs - Game Structure
16:24
Tarodev
Рет қаралды 191 М.
The Weird History of JavaScript
12:09
Fireship
Рет қаралды 1,2 МЛН
Raycast Unity 3d - unity raycast tutorial
5:40
Pixelbug Studio
Рет қаралды 48 М.
Coding Adventure: Portals
16:06
Sebastian Lague
Рет қаралды 1,3 МЛН
How To Build An Event System in Unity
8:01
Game Dev Guide
Рет қаралды 408 М.
Photorealism in Blender: Unlocked
4:18
Scenefiller
Рет қаралды 149 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 58 МЛН