Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 20, 2013 at 02:35 AM by Eric5h5 for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by Heu · Feb 20, 2013 at 02:21 AM · game

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;
         
     }
 
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Professor Snake · Feb 20, 2013 at 02:27 AM 0
Share

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.

avatar image Eric5h5 · Feb 20, 2013 at 02:36 AM 0
Share

This site is for specific development questions that have objective answers, not discussion; you can use the forums for that.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges