Raycasting with Finite State Machine?

I still can’t figure how dumb is the question, but that’s why I’m asking here.

In my game the player has a forward raycast which recognizes the name of the object that collides with. In a previous similar game I wrote a lot of “if” conditions (if the hit name is this, do this, if the hit name is that, do that) like a lot of times.
My question is: referring to the coding level does this suck for the computer? Could a State Machine provide brighter results? I didn’t have any hard problem with frame rates and stuff, but I’m wondering which is the “more efficient” way.
Thanks for your time!

If you are doing different logic for each possible object “name”, there’s only a slightly cleaner/more efficient way to do this, and that is to use a switch statement instead of an if statement since switch statements are compiled to jump tables at runtime.

E.g.:

string objectName = gameObject.name;

// Will perform every check until it finds your object name (slower).
if (objectName == "foo")
{
}
else if (objectName == "bar")
{
}

// Will jump immediatley to your object name (faster!).
switch (objectName)
{
    case "foo":
        break;
    case "bar":
        break;
}

Thanks a lot! Do you think is worth using this method of the “if” collection should be fine anyway? :slight_smile: