Question on performance on code structure

Hi everyone,
I am puzzled by how the structure for these chapters have been architected, specifically for the lesson “Implementing Action Priority”. Wasn’t it better in terms of performance to have a simple raycast in the update loop, and depending on the Tag of the object that has been hit, implement a switch case?
The way you are doing is to getComponent at every frame to check if there’s a CombatTarget, and if that’s null, you ignore it. Wouldn’t it be better to make the raycast upon the mouse click, and only then check if the tag corresponds to enemy/floor?

thanks for your attention!

1 Like

This is a very good question. Performance is an issue in these types of games, and there is no doubt that raytraces are an expensive operation.
The reason we’re doing this now is that later in the course we are going to be implementing affordances, which is a fancy way of saying that we’ll be changing the cursor to reflect what the pointer is over. In order to properly do this, we’ll need to perform a raycast every frame.
That being said, if the game is being designed for mobile then affordances don’t really apply (no mouse, or mouse cursor for that matter). In that case, you would want to setup your code exactly as you suggest, only raycasting when a touch is detected.

Thank you very much for your reply Brian, it makes definitely sense to have it for dynamic cursors then.

This topic was automatically closed after 21 hours. New replies are no longer allowed.

Privacy & Terms