I first got introduced to coding with Ruby and now I am learning how to create games in C# with Unity.
I am following a tutorial and I refactored the code to make it easier for me to grasp. I am really curious to know the opinion of more advanced programmers on the following code.
Here is the initial code:
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour{
private Rigidbody2D playerRigidBody2D;
private float movePlayerHorizontal;
private float movePlayerVertical;
private Vector2 movement;
public float speed = 4.0f;
private Animator playerAnim;
private SpriteRenderer playerSpriteImage;
void Awake(){
playerRigidBody2D = (Rigidbody2D)GetComponent(typeof(Rigidbody2D));
playerAnim=(Animator)GetComponent(typeof(Animator));
playerSpriteImage=(SpriteRenderer)GetComponent(typeof(SpriteRenderer));
}
void Update () {
movePlayerHorizontal = Input.GetAxis("Horizontal");
movePlayerVertical = Input.GetAxis("Vertical");
movement=new Vector2(movePlayerHorizontal,movePlayerVertical);
playerRigidBody2D.velocity=movement*speed;
if(movePlayerVertical==0 && movePlayerHorizontal==0){
playerAnim.SetBool("moving",false);
}else{
playerAnim.SetBool("moving",true);
}
if(movePlayerVertical!=0){
playerAnim.SetBool("xMove",false);
playerSpriteImage.flipX=false;
if(movePlayerVertical>0){
playerAnim.SetInteger("yMove",1);
}else if(movePlayerVertical<0){
playerAnim.SetInteger("yMove",-1);
}
}else {
playerAnim.SetInteger("yMove",0);
if(movePlayerHorizontal>0){
playerAnim.SetBool("xMove",true);
playerSpriteImage.flipX=false;
}else if(movePlayerHorizontal<0){
playerAnim.SetBool("xMove",true);
playerSpriteImage.flipX=true;
}else{
playerAnim.SetBool("xMove",false);
}
}
}
}
All the logic is placed inside of the #Update
method, and there are no private methods to help self-document/declutter it.
This is how I refactored the code:
using System.Collections;
using UnityEngine;
public class CharacterMovement : MonoBehaviour {
private Rigidbody2D _playerRigidbody2D;
private Animator _playerAnimator;
private SpriteRenderer _playerSpriteRenderer;
private float movePlayerHorizontal;
private float movePlayerVertical;
private Vector2 movement;
public float speed = 4.0f;
void Awake() {
_playerRigidbody2D = (Rigidbody2D)GetComponent(typeof(Rigidbody2D));
_playerAnimator = (Animator)GetComponent(typeof(Animator));
_playerSpriteRenderer = (SpriteRenderer)GetComponent(typeof(SpriteRenderer));
}
void Update () {
movePlayer();
detectMovementDirection();
}
private void detectMovementDirection() {
if(isMovingVertical()) {
moveVertical();
} else {
moveHorizontal();
}
}
private void moveHorizontal(){
_playerAnimator.SetInteger("yMove", 0);
if (isMovingRight()) {
_playerAnimator.SetBool("xMove", true);
_playerSpriteRenderer.flipX = false;
} else if (isMovingLeft()) {
_playerAnimator.SetBool("xMove", true);
_playerSpriteRenderer.flipX = true;
} else {
_playerAnimator.SetBool("xMove", false);
}
}
private void moveVertical(){
_playerAnimator.SetBool("xMove", false);
_playerSpriteRenderer.flipX = false;
if(isMovingUp()) {
_playerAnimator.SetInteger("yMove", 1);
} else if (isMovingDown()) {
_playerAnimator.SetInteger("yMove", -1);
}
}
private bool isMovingVertical() {
return movePlayerVertical != 0;
}
private bool isMovingUp() {
return movePlayerVertical > 0;
}
private bool isMovingDown() {
return movePlayerVertical < 0;
}
private bool isMovingRight() {
return movePlayerHorizontal > 0;
}
private bool isMovingLeft() {
return movePlayerHorizontal < 0;
}
private void movePlayer(){
movePlayerHorizontal = Input.GetAxis("Horizontal");
movePlayerVertical = Input.GetAxis("Vertical");
movement = new Vector2(movePlayerHorizontal, movePlayerVertical);
_playerRigidbody2D.velocity = movement * speed;
}
}
The code is exactly the same, but I broke it up in smaller methods(a la Ruby) and the #Update
method, now consists of only 2 calls which in my opinion, makes the code easier to reason about.
Is there an experienced C# programmer that can enlighten me if 1 of the coding style is better and if so why?
moving
property updated? \$\endgroup\$