Raycasts in Unity (made easy)

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ม.ค. 2025

ความคิดเห็น • 99

  • @MyPing0
    @MyPing0 2 ปีที่แล้ว +34

    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 ปีที่แล้ว +19

      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 ปีที่แล้ว +1

      ​@@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  2 ปีที่แล้ว +4

      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 2 ปีที่แล้ว +2

      @@GameDevBeginner Wow dude thanks so much for the follow up! I'm gonna read this right now, you are so awesome!

    • @hello.4693
      @hello.4693 3 หลายเดือนก่อน

      @@GameDevBeginner So... you are telling me Debug.Log("Example"); is creating everytime a new string? sounds like its smarter to declare few strings before runtime and use them as errors isnt it?

  • @jatgreen2995
    @jatgreen2995 2 ปีที่แล้ว +30

    if only all tutorials were this good, concise and comprehensive with timestamps for whatever you're looking for. you've absolutely mastered tutorial creation

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 2 ปีที่แล้ว +22

    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.

  • @rishi76
    @rishi76 5 หลายเดือนก่อน +1

    Can't believe it took TH-cam such a long time to recommend this channel to me!!!

  • @MellioMusic
    @MellioMusic 2 ปีที่แล้ว +5

    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!

  • @Rovsau
    @Rovsau 2 ปีที่แล้ว +2

    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!

  • @martir1786
    @martir1786 5 หลายเดือนก่อน +4

    PLEASE do a video like this on Angles and rotations. WE NEED IT SO BAD

  • @oyleolduboyleoldu3677
    @oyleolduboyleoldu3677 27 วันที่ผ่านมา

    this is the golden raycast documentary

  • @ВладФишер-п6л
    @ВладФишер-п6л ปีที่แล้ว

    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!

  • @ampomahwinston3436
    @ampomahwinston3436 ปีที่แล้ว

    thanks a lot, timeline: 5:00 - 5:10, literally assisted me in an issue have been facing for 2 and half weeks.

  • @neurotrollium253
    @neurotrollium253 ปีที่แล้ว +1

    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.

  • @zaidmermam2524
    @zaidmermam2524 2 ปีที่แล้ว +4

    thats awesome man! your explaination is awesome, the quality is awesome, the sound is crisp, the visuals are cool, great job!

  • @notevenjuice1565
    @notevenjuice1565 2 ปีที่แล้ว +1

    Super underrated channel. Instruction was perfect thank you so much.

  • @techno_tuna
    @techno_tuna 2 ปีที่แล้ว

    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.

  • @kiel5390
    @kiel5390 2 ปีที่แล้ว +3

    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

  • @MuhammadHosny0
    @MuhammadHosny0 2 ปีที่แล้ว

    Good job mentioning Non-Alloc it can be a life saver.

  • @KingDoodies
    @KingDoodies 4 หลายเดือนก่อน

    thank you sir after 2 minutes my entire raycast was fixed

  • @MellowR6
    @MellowR6 5 หลายเดือนก่อน

    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)

  • @alpercizmeci4039
    @alpercizmeci4039 ปีที่แล้ว +1

    This is some god-tier tutorial. Thanks man keep it up!

  • @simonvutov7575
    @simonvutov7575 ปีที่แล้ว

    Really good video man!! Been using unity for 6 years, but always need a reminder on these things every once in a while lol.

  • @thecopymaker2664
    @thecopymaker2664 ปีที่แล้ว

    Id used Raycasts in Unreal but never Unity, while being almost the same thing, I was struggling, this helped alot!!

  • @גיאקריוף
    @גיאקריוף 9 หลายเดือนก่อน

    A really great tutorial man, really helped me understand more about raycasts! subbed for more :)

  • @NanoGamingGamer
    @NanoGamingGamer 8 หลายเดือนก่อน +1

    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.

  • @zappist751
    @zappist751 ปีที่แล้ว +15

    The fact that this video is free to watch is unreal...

    • @mattmelbourne3688
      @mattmelbourne3688 10 หลายเดือนก่อน +2

      People in this community generally want eachother to succeed

    • @JacqueCricket
      @JacqueCricket 9 หลายเดือนก่อน +5

      Not Unreal, Unity

    • @burgerdolf
      @burgerdolf 3 หลายเดือนก่อน

      @@JacqueCricketXD

  • @nahuelhemsi
    @nahuelhemsi 9 หลายเดือนก่อน

    This content is underrated

  • @amcmahon134
    @amcmahon134 9 หลายเดือนก่อน +1

    This has helped me so much, thank you!

  • @estam7264
    @estam7264 2 ปีที่แล้ว

    Your videos are awesome man! Seriously the best unity tutorials on TH-cam (I just started using unity so take this with a bit of salt)

  • @333protoman
    @333protoman ปีที่แล้ว

    Outstanding explanation on raycasts

  • @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  ปีที่แล้ว +1

      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!

  • @patrickbielau6919
    @patrickbielau6919 หลายเดือนก่อน

    The first time I saw an error in your videos! (typo in the word SPHERECAST)
    So you are not a machine 😅
    I love your content! Your ways of explaining, showing and suggesting are the best since @Brackeys left the scene 😊
    Edit: typo 😇

  • @joemitchell7420
    @joemitchell7420 2 ปีที่แล้ว

    this is exactly (and i mean exactly) what i needed thank you so much :D

  • @فوزيهالرشيدي-ك2ز
    @فوزيهالرشيدي-ك2ز ปีที่แล้ว

    so clear 👌🏼,I like the way you explain

  • @EltonN.n
    @EltonN.n 8 หลายเดือนก่อน

    Great video, thanks, your didactic is amazing

  • @Director414
    @Director414 ปีที่แล้ว

    Great video! SUper awesome, many thanks! :) Keep up the good work!

  • @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  ปีที่แล้ว +1

      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!

  • @gctsquaternion6510
    @gctsquaternion6510 7 หลายเดือนก่อน

    Amazing... thank you!

  • @otrqffaimajg
    @otrqffaimajg 11 หลายเดือนก่อน

    Best explanation!

  • @timothy_synth
    @timothy_synth ปีที่แล้ว

    This video is fantastic, thank you 👍🏻

  • @EigenA
    @EigenA ปีที่แล้ว

    Great work

  • @random_precision_software
    @random_precision_software 5 หลายเดือนก่อน

    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

  • @MrZtapp
    @MrZtapp ปีที่แล้ว

    Very good thanks

  • @thebumblecrag61
    @thebumblecrag61 2 ปีที่แล้ว +1

    Fantastic 🎉

  • @marcinziajkowski3870
    @marcinziajkowski3870 2 หลายเดือนก่อน

    How to hit Mesh Collider which is not convex?

  • @muhammadzubairirshad6724
    @muhammadzubairirshad6724 ปีที่แล้ว

    Just Amazing

  • @NikELbErGErBergel
    @NikELbErGErBergel ปีที่แล้ว

    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  ปีที่แล้ว

      Add a collider, set it as a trigger and make sure that the raycast you use queries trigger interactions

  • @patpatmo3171
    @patpatmo3171 ปีที่แล้ว

    nice tutorial, thanks

  • @MarekNijaki
    @MarekNijaki 2 ปีที่แล้ว

    Awesome video

  • @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

  • @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.

  • @random_precision_software
    @random_precision_software 5 หลายเดือนก่อน

    I was hoping for answers for canvas raycast 😢

  • @batnikelam-mavzer443
    @batnikelam-mavzer443 2 ปีที่แล้ว

    Thanks 4 the video

  • @xxxxx409
    @xxxxx409 ปีที่แล้ว

    colliders or raycast for 2d pixel art game ?

  • @affenaffgaffel8330
    @affenaffgaffel8330 2 ปีที่แล้ว

    Hi, Great Video but I have one Question: how can I Calculate the direction from the Origin towards where the Raycast hit?

    • @GameDevBeginner
      @GameDevBeginner  2 ปีที่แล้ว +1

      Vector3 direction = (RaycastHit.point - Ray.origin).normalized; should work.

    • @affenaffgaffel8330
      @affenaffgaffel8330 2 ปีที่แล้ว

      Thanks it works perfectly now

  • @brutosmomentos7355
    @brutosmomentos7355 ปีที่แล้ว

    Does anyone know the color theme he is using for the sytanx?

  • @Hadys
    @Hadys ปีที่แล้ว

    good video thanks

  • @macademia_p7853
    @macademia_p7853 2 ปีที่แล้ว

    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.

  • @TricoliciSerghei
    @TricoliciSerghei ปีที่แล้ว

    The video is great thank you.. But I'd call it, Thorough Raycasts in Unity.

  • @furizagold
    @furizagold ปีที่แล้ว

    thx sis

  • @jelleraffel5
    @jelleraffel5 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      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.

  • @Blurgert
    @Blurgert 9 หลายเดือนก่อน +1

    I still don't get it

  • @abg2487
    @abg2487 2 ปีที่แล้ว

    How to make ping pong game in unity?

  • @ChapCanai
    @ChapCanai ปีที่แล้ว

    if this was easy then I don't think I wanna go advanced

  • @BorisAverin
    @BorisAverin ปีที่แล้ว

    Супер

  • @ArleneIraba
    @ArleneIraba หลายเดือนก่อน

    Book , на універзітї

  • @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  ปีที่แล้ว +1

      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 😆😅

  • @janisyoutube
    @janisyoutube ปีที่แล้ว

    Just use .Any() instead of .Length > 0.

  • @hello.4693
    @hello.4693 3 หลายเดือนก่อน

    As far as i know unity, layers + raycast (or atleast overall layers) are the most, unnecessarily overcomplicated bs. They should be waaaaay easier.