I am writing code for games in Unity. I should write the part very often and would like to know the flaws of the piece of code. It seems like everything is fine, but nevertheless I feel that some parts may be wrong. I am interested whether my style of coding is good or bad and would like to hear from someone who see my code for the first time whether he or she understands it or not.
Here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
private enum Turned
{left, right};
private Turned turned;
public float walkingSpeed, runningSpeed, currentSpeed;
public Transform playerTransform;
public PlayerAnimationController playerAnimationController;
public SpriteRenderer playerSpriteRenderer;
void Start ()
{
initializeNeededForMovementVariables();
}
private void initializeNeededForMovementVariables()
{
turned = Turned.left;
}
void Update ()
{
moveHorizontallyAndAnimateMovementIfNecessary();
}
private void moveHorizontallyAndAnimateMovementIfNecessary()
{
float moveHorizontallyValue = Input.GetAxis("Horizontal");
if (moveHorizontallyValue == 0)
{
playAnimationOfBeingIdle();
return;
}
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{ currentSpeed = runningSpeed; }
else
{ currentSpeed = walkingSpeed; }
conductHorizontalTranslationOfThePlayer(moveHorizontallyValue);
flipFacingIfNecessary(moveHorizontallyValue);
playAnimationOfHorizontalMovement();
}
private void playAnimationOfBeingIdle()
{
playerAnimationController.beingOnlyIdling();
}
private void playAnimationOfHorizontalMovement()
{
if (currentSpeed == walkingSpeed)
{ playerAnimationController.beingOnlyWalking(); }
else if(currentSpeed == runningSpeed)
{ playerAnimationController.beingOnlyRunning(); }
}
private void flipFacingIfNecessary(float moveHorizontallyValue)
{
if (moveHorizontallyValue > 0 && turned == Turned.left)
{
toggleFlipXOnPlayerRenderer();
turned = Turned.right;
}
else if(moveHorizontallyValue < 0 && turned == Turned.right)
{
toggleFlipXOnPlayerRenderer();
turned = Turned.left;
}
}
private void toggleFlipXOnPlayerRenderer()
{
playerSpriteRenderer.flipX = !playerSpriteRenderer.flipX;
}
private void conductHorizontalTranslationOfThePlayer(float moveHorizontallyValue)
{
transform.Translate(moveHorizontallyValue * currentSpeed, 0, 0);
}
}
Will appreciate any attention and comments. If this question is not appropriate for the site and should be in game overflow, please, move it there.