Why we are doing RaycastAll on every frame and not only on Click?

Hey guys,
Any idea why in this lecture, we are not doing first the check if there is a click.
And only after that to do the Raycasting ?
Won’t this be better from a performance point of view ?
In the video you are doing var hits = Physics.RaycastAll(GetMouseRay()); in every Update invoke.
And only if the result have something you are doing a check if there is a click.
This will result a ton of wasted calculations and sound strange to me.

Correct me if I am wrong please.

3 Likes

I think it’s for the possibility to add further features that you may need it for (e.g. highlight targetable entities, hover-over name display, etc). But sure, if you’re not planning to add other modules, it’s a net resource saver.

1 Like

Yeah as I continued watched the course I saw why they did it.
Just recommend if anyone have the same question as me - just do it like that and continue watching the course. It will make more sense later.

Oops, I missed this question somehow, sorry about that. emGames has it dead on, we’ll be adding cursor affordances which require the mouse position to be polled every frame. On small scenes, this isn’t really too much of a performance hit. If you are developing your game for mobile (which we don’t officially support, but it’s been done), then you’ll want to remove the raytrace and cursor affordances altogether, but still leave in the eventual HandleRaycast(). Instead, you’ll gracefully exit PlayerController’s Update() if there isn’t a touch currently active.

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms