# How to optimize the player movement code like in Tomb Of The Musk game?

I've written some code that performs movement similar to the Tomb Of The Musk game. The idea is that the objet should move in a certain direction until it encounters an obstacle. After that, the player can choose which way the object should move.

The code I wrote seems to me to be rather unoptimized. I think it could have been done quite a bit more rationally than I did.

Object movement code:

[SerializeField]
float speed = .9f;

[SerializeField]

Vector3 targetPosition;
Vector3 moving_direction;

bool movingHorizontally;
bool canCheck;

void Start()
{
targetPosition = transform.position;
}

void Update()
{
if (movingHorizontally)
else

if (canCheck)
{
if (Input.GetAxisRaw("Horizontal") != 0)
{
if (Input.GetAxisRaw("Horizontal") > 0)
{
moving_direction = Vector3.right;
}
else
{
moving_direction = Vector3.left;
}

targetPosition = GetTargetPosition();
movingHorizontally = true;

}

else if (Input.GetAxisRaw("Vertical") != 0)
{
if (Input.GetAxisRaw("Vertical") > 0)
{
moving_direction = Vector3.forward;
}
else
{
moving_direction = Vector3.back;
}

targetPosition = GetTargetPosition();
movingHorizontally = false;
}
}
}

void FixedUpdate()
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);
}

Vector3 GetTargetPosition()
{

float ray_lenght = 1;
while(true)
{
break;

ray_lenght += 1;
}

return transform.position + moving_direction * (ray_lenght-1);
}


Do you have any ideas regarding the improvement of my code?

• Welcome to Code Review! You could improve your question by explaining what "similar to the Tomb Of The Musk game" means, if you want your question to be meaningful to more reviewers, and improve your chance of getting a good review. Also, "how to optimize" is already conveyed by the performance tag, so no need to duplicate that in your title. Commented Nov 6, 2023 at 15:51

In terms of performance what I have done is removed unnecessary method calls and helped out your branch predictor by removing if/else statements.

What I have mainly done though is to enhance code readability which will make it much easier to find performance bottlenecks. I have left your logic intact as I don't have enough information to change it.

Also I noticed you have a call to Vector3.left. Have you implemented your own Vector3 class?

[SerializeField]
float speed = .9f;

[SerializeField]

Vector3 targetPosition;
Vector3 moving_direction;

bool movingHorizontally;
bool canCheck;

void Start()
{
targetPosition = transform.position;
}

void Update()
{
if (!CanMove()) return;

var horizontalRawAxis = Input.GetAxisRaw("Horizontal");
var verticalRawAxis = Input.GetAxisRaw("Vertical");

(moving_direction, movingHorizontally) = horzontalRawAxis > 0? (Vector3.right, true)
: horizontalRawAxis < 0 ? (Vector3.left, true)
: verticalRawAxis > 0 ? (Vector3.forward, false)
: (Vector3.back, false);

targetPosition = GetTargetPosition();
}

void FixedUpdate()
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed);
}

Vector3 GetTargetPosition()
{
float ray_lenght = 1;

while(!PhysicsRaycast(moving_direction, ray_lenght)) ray_lenght += 1;

return transform.position + moving_direction * (ray_lenght-1);
}

bool CanMove(MovementType movementType) => movementType switch
{
MovementType.Left => PhysicsRaycast(Vector3.left, .6f);
MovementType.Right => PhysicsRaycast(Vector3.right, .6f);
MovementType.Forward => PhysicsRaycast(Vector3.forward, .6f);
MovementType.BackWard => PhysicsRaycast(Vector3.back, .6f);

_ => throw new Exception("Unrecognised movement type");
};

bool CanMove(){
return movingHorizontally? CanMove(MovementType.Left) || CanMove(MovementType.Right)
: CanMove(MovementType.Forward) || CanMove(MovementType.BackWard);
}

bool PhysicsRaycast(Vector3 moving_direction, float ray_length) =>

enum MovementType{
Left,
Right,
Forward,
BackWard,
}

• I think your code changes are really good, however Unity doesn't like them, it says there are a lot of bugs when there aren't any, do you have any advice on this? Commented Nov 7, 2023 at 7:21
• I don't know how to insert a picture here, so I'll write examples of errors in text: 1. Assets\Mover.cs(31,58): error CS1525: Invalid expression term ';' 2. Assets\Mover.cs(52,48): error CS1525: Invalid expression term 'switch' 3. Assets\Mover.cs(52,48): error CS1519: Invalid token 'switch' in class, struct, or interface member declaration 4. Assets\Mover.cs(54,27): error CS1519: Invalid token '=>' in class, struct, or interface member declaration And there are many more errors like this that I don't know how to deal with. Commented Nov 7, 2023 at 7:36
• @Hanvil What line does the code snippet you shared begin at? Commented Nov 7, 2023 at 17:31
• the code starts at line 7 Commented Nov 7, 2023 at 17:44
• @Hanvil Ok I'd try find the line that your first error is on and fix that first. Errors like this can cascade especially if characters like ';' or omitted. Also what text editor/IDE are you using? Commented Nov 7, 2023 at 19:35