is this a good place to use a case statement?

I am about to start coding when I shoot a gun that when I hit something , something happens , using the raycast if I hit dirt nothing happens if I hit a wall I place a decal, if I hit this mob I take life and have a partical effect such as blood.

would this be better for cpus to place it in a case and check throu one case instead of a bunch of if statements?

You can use a switch statement to pick functionality based upon the value passed in. All switch statements can be refactored into if statements, though switch statements are useful since they make code easier to read and maintain.

There are often performance advantages to using switch statements when there are a number of sequential cases since compilers can bypass lots of conditional checks and branches and resort to a simple numeric offset instead (aka jump).

Refer to the following page on MSDN for further information regarding the switch statement and its syntax:

Edit: Additional Information

According to the following resource the compiler will usually generate jump tables for numeric cases and hash tables to dispatch string cases. An enum value is naturally numeric and so would also correlate to jump tables.

An Example:

public enum ChessPiece {
    Pawn,
    Bishop,
    Knight,
    Rook,
    Queen,
    King
}

public ChessPiece activePiece;

public void Foo() {
    switch (activePiece) {
        case ChessPiece.Pawn:
            Bar();
            break;
        case ChessPiece.Rook:
            Baz();
            break;
    }
}

public bool CanMoveUpwards() {
    switch (activePiece) {
        case ChessPiece.Pawn:   // we can fall through to next
        case ChessPiece.Rook:
        case ChessPiece.Queen:
        case ChessPiece.King:
            return true;        // no need for break for this

        default:                // any other input
            return false;
    }
}

Yes, you need to use a case statement.

RaycastHit hit;
Physics.Raycast(new Ray(position, direction), out hit, distance);
LayerMask layerHit = hit.transform.gameObject.layer;
if (layerHit == mob)
{
    // gameObject.GetComponent<Health>().health -= damage;
}
else if (layerHit == ground)
{
    // place decal at hit.point
}

The important thing, performance wise, with any boolean checks are to order the booleans that will exit the check in order of simplicity of calculation and relative occurrence. An example is when you’re both checking that what you’ve hit is a mob and the distance is under a certain value. It is less computationally expensive to eliminate the possiblity of that case by checking the layermask before the distance, because if it’s not the correct layer then you skip checking the more expensive Vector3.Distance.

As a side note, checking ((position1 - position2).sqrMagnitude < distance*distance) is cheaper than doing Vector3.Distance(position1, position2).

Also, please see Numberkruncher’s answer regarding switch statements. :slight_smile:

Using a switch statement here could actually be very beneficial. Though because our values have to be a constant. You would want to compare the constant values of the LayerMask.

In order to make them a constant first set a LayerMask var as your hit
Then compare the actual value of that to the layermask’s numerical value in your layer list.

that way your case will always be a constant.

                if (Physics.Raycast(ray, out hit))
                {
                    LayerMask layerHit = hit.transform.gameObject.layer;

                    switch (layerHit.value)
                    {
                        case 1:
                            // do stuff
                            break;
                        case 2:
                            // do stuff
                            break;
                    }
                }