Raycasts in Unity (made easy)

Поделиться
HTML-код
  • Опубликовано: 5 фев 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 4 месяца назад

      @@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 года назад +32

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

  • @oyleolduboyleoldu3677
    @oyleolduboyleoldu3677 2 месяца назад +1

    this is the golden raycast documentary

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 2 года назад +23

    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 6 месяцев назад +1

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

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

  • @notevenjuice1565
    @notevenjuice1565 2 года назад +1

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

  • @martir1786
    @martir1786 6 месяцев назад +4

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

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

  • @ВладФишер-п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!

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

  • @KingDoodies
    @KingDoodies 5 месяцев назад

    thank you sir after 2 minutes my entire raycast was fixed

  • @MuhammadHosny0
    @MuhammadHosny0 2 года назад

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

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

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

  • @ampomahwinston3436
    @ampomahwinston3436 Год назад

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

  • @MellowR6
    @MellowR6 6 месяцев назад

    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)

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

  • @alpercizmeci4039
    @alpercizmeci4039 Год назад +1

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

  • @גיאקריוף
    @גיאקריוף 10 месяцев назад

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

  • @thecopymaker2664
    @thecopymaker2664 Год назад

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

  • @amcmahon134
    @amcmahon134 10 месяцев назад +1

    This has helped me so much, thank you!

  • @estam7264
    @estam7264 2 года назад

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

  • @patrickbielau6919
    @patrickbielau6919 3 месяца назад

    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 😇

  • @333protoman
    @333protoman Год назад

    Outstanding explanation on raycasts

  • @nahuelhemsi
    @nahuelhemsi 10 месяцев назад

    This content is underrated

  • @Director414
    @Director414 Год назад

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

  • @NanoGamingGamer
    @NanoGamingGamer 9 месяцев назад +2

    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.

  • @joemitchell7420
    @joemitchell7420 2 года назад

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

  • @فوزيهالرشيدي-ك2ز

    so clear 👌🏼,I like the way you explain

  • @zappist751
    @zappist751 Год назад +17

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

    • @mattmelbourne3688
      @mattmelbourne3688 11 месяцев назад +2

      People in this community generally want eachother to succeed

    • @JacqueCricket
      @JacqueCricket 10 месяцев назад +5

      Not Unreal, Unity

    • @burgerdolf
      @burgerdolf 4 месяца назад

      @@JacqueCricketXD

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

  • @EltonN.n
    @EltonN.n 9 месяцев назад

    Great video, thanks, your didactic is amazing

  • @timothy_synth
    @timothy_synth Год назад

    This video is fantastic, thank you 👍🏻

  • @otrqffaimajg
    @otrqffaimajg Год назад

    Best explanation!

  • @gctsquaternion6510
    @gctsquaternion6510 8 месяцев назад

    Amazing... thank you!

  • @EigenA
    @EigenA Год назад

    Great work

  • @thebumblecrag61
    @thebumblecrag61 2 года назад +1

    Fantastic 🎉

  • @jaylini2250
    @jaylini2250 2 года назад

    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  2 года назад +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 2 года назад

      @@GameDevBeginner Thank you very much!

  • @patpatmo3171
    @patpatmo3171 Год назад

    nice tutorial, thanks

  • @MarekNijaki
    @MarekNijaki 2 года назад

    Awesome video

  • @muhammadzubairirshad6724
    @muhammadzubairirshad6724 Год назад

    Just Amazing

  • @batnikelam-mavzer443
    @batnikelam-mavzer443 2 года назад

    Thanks 4 the video

  • @MrZtapp
    @MrZtapp Год назад

    Very good thanks

  • @Hadys
    @Hadys Год назад

    good video thanks

  • @random_precision_software
    @random_precision_software 6 месяцев назад

    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

  • @TricoliciSerghei
    @TricoliciSerghei Год назад

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

  • @marcinziajkowski3870
    @marcinziajkowski3870 3 месяца назад

    How to hit Mesh Collider which is not convex?

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

  • @furizagold
    @furizagold Год назад

    thx sis

  • @random_precision_software
    @random_precision_software 6 месяцев назад

    I was hoping for answers for canvas raycast 😢

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

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

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

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

  • @xxxxx409
    @xxxxx409 Год назад

    colliders or raycast for 2d pixel art game ?

  • @brutosmomentos7355
    @brutosmomentos7355 Год назад

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

  • @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 10 месяцев назад +1

    I still don't get it

  • @abg2487
    @abg2487 2 года назад

    How to make ping pong game in unity?

  • @BorisAverin
    @BorisAverin Год назад

    Супер

  • @ChapCanai
    @ChapCanai Год назад

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

  • @janisyoutube
    @janisyoutube Год назад

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

  • @ArleneIraba
    @ArleneIraba 2 месяца назад

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

  • @hello.4693
    @hello.4693 4 месяца назад

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