Raycasts in Unity (made easy)

Поділитися
Вставка
  • Опубліковано 23 сер 2024

КОМЕНТАРІ • 88

  • @MyPing0
    @MyPing0 2 роки тому +29

    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 роки тому +15

      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  Рік тому +3

      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

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

  • @jatgreen2995
    @jatgreen2995 2 роки тому +26

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

  • @rishi76
    @rishi76 26 днів тому +1

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

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 Рік тому +21

    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.

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

    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!

  • @zappist751
    @zappist751 7 місяців тому +12

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

    • @mattmelbourne3688
      @mattmelbourne3688 5 місяців тому +1

      People in this community generally want eachother to succeed

    • @JacqueCricket
      @JacqueCricket 4 місяці тому +2

      Not Unreal, Unity

  • @kiel5390
    @kiel5390 Рік тому +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

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

  • @notevenjuice1565
    @notevenjuice1565 Рік тому +1

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

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

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

  • @TheMellowed
    @TheMellowed 23 дні тому

    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 Місяць тому

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

  • @neurotrollium253
    @neurotrollium253 9 місяців тому +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.

  • @MuhammadHosny0
    @MuhammadHosny0 Рік тому

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

  • @ampomahwinston3436
    @ampomahwinston3436 Рік тому

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

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

  • @amcmahon134
    @amcmahon134 5 місяців тому +1

    This has helped me so much, thank you!

  • @nahuelhemsi
    @nahuelhemsi 5 місяців тому

    This content is underrated

  • @user-lt4go4qb4b
    @user-lt4go4qb4b 4 місяці тому

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

  • @333protoman
    @333protoman Рік тому

    Outstanding explanation on raycasts

  • @estam7264
    @estam7264 2 роки тому

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

  • @NanoGamingGamer
    @NanoGamingGamer 3 місяці тому +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.

  • @otrqffaimajg
    @otrqffaimajg 7 місяців тому

    Best explanation!

  • @user-yq9ro8hx5m
    @user-yq9ro8hx5m Рік тому

    so clear 👌🏼,I like the way you explain

  • @eltinhonunes
    @eltinhonunes 4 місяці тому

    Great video, thanks, your didactic is amazing

  • @Director414
    @Director414 9 місяців тому

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

  • @joemitchell7420
    @joemitchell7420 Рік тому

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

  • @gctsquaternion6510
    @gctsquaternion6510 3 місяці тому

    Amazing... thank you!

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

  • @timothy_synth
    @timothy_synth 11 місяців тому

    This video is fantastic, thank you 👍🏻

  • @MrZtapp
    @MrZtapp 10 місяців тому

    Very good thanks

  • @thebumblecrag61
    @thebumblecrag61 Рік тому +1

    Fantastic 🎉

  • @EigenA
    @EigenA Рік тому

    Great work

  • @muhammadzubairirshad6724
    @muhammadzubairirshad6724 Рік тому

    Just Amazing

  • @TricoliciSerghei
    @TricoliciSerghei 9 місяців тому

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

  • @patpatmo3171
    @patpatmo3171 Рік тому

    nice tutorial, thanks

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

  • @MarekNijaki
    @MarekNijaki 2 роки тому

    Awesome video

  • @batnikelam-mavzer443
    @batnikelam-mavzer443 2 роки тому

    Thanks 4 the video

  • @Hadys
    @Hadys Рік тому

    good video thanks

  • @random_precision_software
    @random_precision_software 23 дні тому

    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 25 днів тому

    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.

  • @furizagold
    @furizagold Рік тому

    thx sis

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

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

  • @xxxxx409
    @xxxxx409 Рік тому

    colliders or raycast for 2d pixel art game ?

  • @ChapCanai
    @ChapCanai 9 місяців тому

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

  • @BorisAverin
    @BorisAverin 9 місяців тому

    Супер

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

  • @brutosmomentos7355
    @brutosmomentos7355 Рік тому

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

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

  • @abg2487
    @abg2487 2 роки тому

    How to make ping pong game in unity?

  • @janisyoutube
    @janisyoutube Рік тому

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

  • @Blurgert
    @Blurgert 4 місяці тому

    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  Рік тому +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 😆😅