Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Miki_505 · Feb 14, 2020 at 01:07 PM · scripting problemoptimizationvisual studiooopsolid

Refactoring code for S.O.L.I.D principles

I want to learn SOLID but its really hard for me. I don' t know where to start. After watching materials and still not being able to refactor my own code I decided to ask here. How would you refactor such a code. Seeing an example of someone more experienced would mean a lot to me.

Thanks in advice :)

 public class Player : MonoBehaviour
 {
     [SerializeField] [Range(0f, 1000f)] float movementSpeed = 654;
     [SerializeField] [Range(0f, 60f)] float playerControlledJumpForce = 30.3f;
     [SerializeField] [Range(1f, 40f)] float timesJumpForceAtFrameIsSmaller = 31.25f;
     [SerializeField] [Range(0f, 60f)] float fromEnemyJumpForce = 15.9f;
     [SerializeField] int maxJumps = 1;
     [SerializeField] [Range (0f, 1f)] float minTimeBetweenJumps = 0.005f;
     float playerAtFrameControlledJumpForce;
 
     //private IJump; 
     
 
     private int jumpsLeft;
     private bool isJumping = false;
 
 
     float lastJumpTime = 0;
 
     new Rigidbody rigidbody;
 
     SessionManager sessionManager;
     
 
 
     // Start is called before the first frame update
     void Start()
     {
         sessionManager = FindObjectOfType<SessionManager>();
 
         rigidbody = GetComponent<Rigidbody>();
 
         jumpsLeft = maxJumps;
 
         playerAtFrameControlledJumpForce = playerControlledJumpForce / timesJumpForceAtFrameIsSmaller;
     }
 
     // Update is called once per frame
     void Update()
     {     
        PlayerControlledJump();
 
        HorizontalMovement();
     }
 
     private void Jump(float jumpForce)
     {      
         Vector3 jumpVelocityToAdd = new Vector3(rigidbody.velocity.x, jumpForce,0);
         rigidbody.velocity += jumpVelocityToAdd;
     }
 
     private void PlayerControlledJump()
     {      
         if (Input.GetKeyDown(KeyCode.Space)
             && jumpsLeft > 0)
         {
             #region
             if (!(Time.time - lastJumpTime >= minTimeBetweenJumps))
             {
                 return;
             }
             lastJumpTime = Time.time;
             #endregion
 
             jumpsLeft--;
 
             isJumping = true;
 
             Jump(playerControlledJumpForce);
         }
         if (Input.GetKey(KeyCode.Space) && isJumping)
         {
             Jump(playerAtFrameControlledJumpForce);
         }
         if (Input.GetKeyUp(KeyCode.Space) || rigidbody.velocity.y < 0f)
         {
             isJumping = false;
         }
     }
     private void HorizontalMovement() // TODO, take it out to separete class for SOLID principles
     {
         float movementScalar = movementSpeed * Time.deltaTime;
         
         rigidbody.velocity = new Vector3(Input.GetAxisRaw("Horizontal") * movementScalar, rigidbody.velocity.y, rigidbody.velocity.z);
     }
 
     private void OnCollisionEnter(Collision collision)
     {
 
         if (collision.gameObject.tag == "Enemy")
         {
             GameOver();
           
         }
 
         if(collision.gameObject.tag == "DeathCollider")
         {
             Jump(fromEnemyJumpForce);
             
         }
 
 
         if (collision.gameObject.layer == 8)
         {
             jumpsLeft = maxJumps;
         }
         else if(collision.gameObject.layer == 9)//Zero Level layer
         {
             GameOver();
             
         }
     }
     private void OnCollisionExit(Collision collision)
     {
         if(collision.gameObject.layer == 8) // Ground layer
         {
             jumpsLeft = 0;
         }
         
     }
     private void GameOver()
     {
         Debug.Log("GAME OVER");
         sessionManager.ReloadCurrentScene();
         Destroy(gameObject);
         //TODO proceed game over
     }
 }
 



Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

295 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A more optimized way to adjust duplicate integer values In a list? 0 Answers

need some help clarifying with Isometric and Orthographic and errors doesn't show up in VS but it does in Unity 1 Answer

Performance question: Is it better to Instantiate particle systems OR use same particle system? 1 Answer

Change in Editor via Script Values of another Script 2 Answers

Updating Visual Studio Editor broke all my code. 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