Optimizing this Method

Hey Guys, I’m new to this and this is my first question:), but just wanted to ask a question on how i could make the method below more optimized? is it Possible? because i see a lot of repetition in code.
Basically what this method does in the game is moves the player to a position to the right or left depending witch way the phone is tilted and witch way the player is facing towards, and it moves player back to center when the phone is held straight.

any suggestions would be appreciated thanks.

`void TiltPlayer(bool? isLeft)
{

        hasTilted = true;
        switch (Dir)
        {
            case Directions.North:
                switch (isLeft)
                {
                    case true:
                        isLeftTilt = true;

                        player.transform.position = new Vector3(player.transform.position.x - playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                        break;
                    case false:
                        isRightTilt = true;
                        player.transform.position = new Vector3(player.transform.position.x + playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                        break;
                    default:
                        if (isLeftTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x + playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                            isLeftTilt = false;
                        }
                        else if (isRightTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x - playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                            isRightTilt = false;
                        }
                        break;
                }
                break;
            case Directions.South:
                switch (isLeft)
                {
                    case true:
                        isLeftTilt = true;

                        player.transform.position = new Vector3(player.transform.position.x + playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                        break;
                    case false:
                        isRightTilt = true;
                        player.transform.position = new Vector3(player.transform.position.x - playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                        break;
                    default:
                        if (isLeftTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x - playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                            isLeftTilt = false;
                        }
                        else if (isRightTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x + playerOffset,
                        player.transform.position.y,
                        player.transform.position.z);
                            isRightTilt = false;
                        }
                        break;
                }
                break;
            case Directions.East:
                switch (isLeft)
                {
                    case true:
                        isLeftTilt = true;

                        player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z + playerOffset);
                        break;
                    case false:
                        isRightTilt = true;
                        player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z - playerOffset);
                        break;
                    default:
                        if (isLeftTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z - playerOffset);
                            isLeftTilt = false;
                        }
                        else if (isRightTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z + playerOffset);
                            isRightTilt = false;
                        }
                        break;
                }
                break;
            case Directions.West:
                switch (isLeft)
                {
                    case true:
                        isLeftTilt = true;

                        player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z - playerOffset);
                        break;
                    case false:
                        isRightTilt = true;
                        player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z + playerOffset);
                        break;
                    default:
                        if (isLeftTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z + playerOffset);
                            isLeftTilt = false;
                        }
                        else if (isRightTilt)
                        {
                            player.transform.position = new Vector3(player.transform.position.x,
                        player.transform.position.y,
                        player.transform.position.z - playerOffset);
                            isRightTilt = false;
                        }
                        break;
                }
                break;
        }
}`

First of all, don’t switch on true/false statements. Use if/elses. A single if statement will, on average, run faster than a switch with two cases. Secondly, why is your bool argument nullable (bool? means that C# should use the nullable form of bool)?

Also, what is setting the Directions values? Why not just use Gyroscope.attitude to get the angles, then shift the player in the direction of the rotation? Try something like this:

Vector3 angles = Gyroscope.attitude.eulerAngles;
float forwardBack;
float leftRight;

if(angles.x > 5.0f)// The reason for this is that it is incredibly difficult to get a gyroscope on a phone COMPLETELY balanced, so you will probably want to give a bit of leeway
    forwardBack = playerOffset;
else if(angles.x < -5.0f)
    forwardBack = -playerOffset;
else
    forwardBack = 0.0f;

if(angles.y > 5.0f)
    leftRight = playerOffset;
else if(angles.y < -5.0f)
    leftRight = -playerOffset;
else
    leftRight = 0.0f;

player.transform.position += new Vector3(leftRight, 0.0f, forwardBack);

A slight side-note on coding conventions, however: When you code, you should be concerned with three aspects of the code, in a very particular order. Those aspects are:

  1. Readability: Does what I just wrote make good sense to anyone but me (if it doesn’t make sense to you, then you’ve really got a problem)?
  2. Functionality Does what I just wrote work?
  3. Efficiency How well does it work?

Forgive my curtness, but this is a very important topic that can not be emphasized enough. These are ultimately the only three things that any coder really can be concerned with when coding, since they are relatively broad topics. However, the above order is argued by many (myself included) to be the correct order of importance. That is to say if your code is not as readable as it can be reasonably made, you’re doing it wrong. Readability and standards for practice should always come first.

Secondly, if it doesn’t work then you should not be concerned whatsoever with how fast it runs. I can make a program that does absolutely nothing and make it run faster, and given that neither my program nor yours technically accomplish anything, mine is better.

Thirdly, efficiency is a very touchy thing, these days. Generally speaking, if it’s readable, it will be made more efficient by the compiler/interpreter/virtual machine/whatever is running your code than you could POSSIBLY ever make it by micro-managing statements to the point of putting switch statements within switch statements within switch statements.

This is done intentionally to make programs easier to write, read, and maintain, so that software is less buggy and runs faster, because a team of hundreds upon thousands of experts in the field made the software that runs our software over the course of several decades. Use Occam’s razor, and look through common practices and standards for the language you’re writing in, and follow those procedures to a T.

If you’ve hit a bottleneck on performance and want to find a better/faster method of doing something, you may want to do some research. There’s no point reinventing the wheel, if there’s already entire books filled with algorithms that do what you are intending to do that will ultimately do it faster, and in a more standard, and readable way (and I say this from both experience, and from what others have taught/shown me).