Not getting how CameraRaycaster class works at all

Did I miss something? For a spiral syllabus, I think a few lower spirals may have been skipped on this one. I’m currently on lecture 15, which again uses the CameraRaycaster script, but I’m totally lost in how this class does what it does, and Unity help notes on the topic don’t help at all.

I understand bit shifting, but don’t know how bit shifting by 8 returns a suitable result for the walkable layer etc. How does the parameter that is passed in flow through the class execution, and how does “out hit” work, vs “return hit”. Perhaps a whiteboard type flow chart might help in explaining this?

At this stage, and having completed all of unity 3D, and half of Unity 2D courses, downloading scripts and working through explanations is more appropriate than writing the script in course. This does indeed accelerate learning, and save on time invested. But even further explanation of the CameraRaycaster script, than currently available, would be appreciated.

Hello Sorendark, how are you?

Layers in unity are optimized in order to improve performance for physics calculations, that’s why it uses bit shifting and you have to do the transformation as shown in the class, “8” is the number assigned to the “Walkable” layer. When you say the layer that you want to check for, the ray will only collide with this specific layer, that’s how it knows what is under your mouse, you give it an order of preference and it will check one layer after the other to see if any of them has succeeded to hit the intended layer and will return in the first to succeed.

About the “out hit”, I’ll paste what I answered to this same question in the Discord group a few days ago:

Physics.Raycast returns a bool
True if it hits something, false if it doesn’t
Ray is the direction if the raycast, it’s just a utility class to save origin vector3 and destination vector3
Hit is another utility class that raycast uses, it holds information about the object it has hitten and the hit itself (such as a position that it happened)(edited)
The “out” keyword just means that it will change the parameter of type “hit” that you pass to the method
That’s why you need to initialize a hit variable before
This way the raycast method will process the actual raycast, return the bool saying if it has hitten or not, and if it does it will pass the hit information to the variable that you used as parameter

let me know if it does helps to understand how it works.

Privacy & Terms