- Home /
Question is off-topic or not relevant
Messy Code or Clean Code
I've been wondering for awhile now and haven't really considered how my code should look like. I've always just code the way I usually code and never really questioned whether this would be a mess to a professional programmer or not, so I ask this to you. Would you consider my coding mess or clean. If so improvements and tips are nice.
Here is an example of a script (Pretty Long and simple) The script works fine.
using UnityEngine;
using System.Collections;
public class _Player : MonoBehaviour {
private IRagePixel ragePixel;
public enum WalkingState {Running=0, Slide, Jump};
public WalkingState state = WalkingState.Running;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public GameObject respawn;
public Transform arrow;
private Vector3 moveDirection = Vector3.zero;
private bool isJumping;
private bool isSliding = false;
private bool isRunning = false;
private bool canShoot = true;
private bool doubleJump = false;
void Start () {
ragePixel = GetComponent<RagePixelSprite>();
}
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
doubleJump=false;
isJumping=false;
moveDirection = new Vector3(speed, Input.GetAxis("Vertical"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump")){
doubleJump=true;
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if(!controller.isGrounded&&isJumping){
state = WalkingState.Jump;
ragePixel.PlayNamedAnimation("jump",false);
}
if (Input.GetKey(KeyCode.Space)&&!isSliding&&canShoot){
Instantiate(arrow,transform.position,transform.rotation);
canShoot=false;
StartCoroutine("Shoot");
}
if (Input.GetKey(KeyCode.UpArrow))
{
doubleJump=true;
isJumping=true;
state = WalkingState.Jump;
}
else if (Input.GetKey(KeyCode.DownArrow)&&controller.isGrounded)
{
isRunning=false;
if(!isSliding){
controller.radius = 9;
controller.height = 5;
controller.center = new Vector3(23,-23,0);
isSliding=true;
}
state = WalkingState.Slide;
}
else
{
if(!isJumping){
if(!isRunning){
controller.radius = 6;
controller.height = 64;
controller.center = new Vector3(0,0,0);
isRunning = true;
}
isSliding=false;
state = WalkingState.Running;
}
}
switch (state)
{
case(WalkingState.Running):
ragePixel.PlayNamedAnimation("run", false);
break;
case (WalkingState.Jump):
ragePixel.PlayNamedAnimation("jump", false);
break;
case (WalkingState.Slide):
ragePixel.PlayNamedAnimation("slide", false);
break;
}
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag=="hazard"){
this.transform.position = respawn.transform.position;
}
}
IEnumerator Shoot(){
yield return new WaitForSeconds(1f);
canShoot=true;
}
}
That is a fairly subjective question. In the end, whether the code looks good is less important than have it working. (which is also a subjective point of view). PS: It looks clean enough. It is properly indented and the variable names reflect their purpose and usage.
This site is for specific development questions that have objective answers, not discussion; you can use the forums for that.
Follow this Question
Related Questions
How do you script in Java? Anyone have some exaple codes I could use? 0 Answers
How to make an object appear after collecting items? 3 Answers
problem with my simple script please help. 2 Answers
Help Me with My Script Pls?????? 1 Answer
How Cast a ray between two points and return all points intercepted? 0 Answers