Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Longer Monobehavior vs More Monobehaviors (Secondarily, looking for feedback)

Discussion in 'Scripting' started by Aggrojag, Apr 14, 2014.

  1. Aggrojag

    Aggrojag

    Joined:
    Mar 24, 2014
    Posts:
    7
    As a preface, I've only begun a few weeks ago. I was learning C++, until I downloaded Unity about 2 weeks ago and saw that C++ is not included in the list for Monobehaviors. I've since moved over to C#. While studying C++ for that short span, one of the key things that was spoken of in the books and tutorials I watched was that a function should be able able to be summarized without saying "and". Example: It makes the player jump vs It makes the player jump, and then applies gravity.

    With this being said, I spent several hours today fixing my jump script. I noticed by the end of it, that it was rather long. I also noticed, that it seemed like there was a delay when I hit W that wasn't there before (unless my mind made it up!). So, I decided to split the script in to two Monobehaviors. One that added upward force to my object, and one that would apply gravity on a timer system to allow for longer/higher jumps if W was held down. It appeared as if the delay vanished.

    So, my question is, should monobehaviors be treated with the same mindset, as a good function? Or is it situational? Or does it not really matter?

    Here is the script I am referring to, I'm sorry if anything isn't standard as far as the commenting and placement of things go, however I find this the easiest way to read my own scripting.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (JumpGravity))]
    5.  
    6. public class JumpBase : MonoBehaviour {
    7.  
    8.  
    9.  
    10.     private float distToGround;
    11.     private JumpGravity jumpGravity;
    12.  
    13.     //To allow for slopes
    14.     public float skin = 0.25f;
    15.    
    16.     //Height of jump
    17.     public float jumpHeight = 15f;
    18.  
    19.  
    20.     void Start(){
    21.         // get the distance to ground
    22.         distToGround = collider.bounds.extents.y;
    23.         jumpGravity = GetComponent<JumpGravity>();
    24.  
    25.  
    26.     }//Start close
    27.  
    28.     //Is the player grounded?
    29.     bool IsGrounded() {
    30.         return Physics.Raycast(transform.position, -Vector3.up, distToGround + skin);
    31.     }//Grounded Close
    32.    
    33.     void FixedUpdate () {
    34.    
    35.         if (Input.GetKeyDown(KeyCode.W)  IsGrounded ()){
    36.             rigidbody.AddForce(0f, jumpHeight * 100, 0f);
    37. }//Jump origin close
    38.  
    39.  
    40.         if (IsGrounded () == false  jumpGravity.keyDown == false){
    41.             rigidbody.AddForce(Physics.gravity * rigidbody.mass * 3f * 2f);
    42. }//Normal Gravity Close
    43.  
    44.         if (IsGrounded ()) {
    45.             jumpGravity.timer = 0f;
    46.         }//If grounded, reset
    47.  
    48.  
    49. }//FixedUpdate Close
    50. }
    51.  

    and the other script associated with it



    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class JumpGravity : MonoBehaviour {
    5.    
    6.     [HideInInspector]
    7.     public bool keyDown = false;
    8.  
    9.     [HideInInspector]
    10.     public float timer;
    11.    
    12.    
    13.     private float gravityMult =3f;
    14.     private float timerMin = 0.001f;
    15.     private float timerMax = .6f;
    16.    
    17.    
    18.     void FixedUpdate () {
    19.        
    20.         if (Input.GetKeyDown (KeyCode.W)) {
    21.             keyDown = true;
    22.         }//if close
    23.        
    24.         else if (Input.GetKeyUp (KeyCode.W)) {
    25.             keyDown = false;       
    26.         }//Else if close
    27.        
    28.         if (keyDown == true) {
    29.             timer += Time.deltaTime;   
    30.         }//Timer set close
    31.        
    32.         if (timer > timerMin  timer < timerMax  keyDown == true) {
    33.             rigidbody.AddForce(Physics.gravity * rigidbody.mass * gravityMult);
    34.         }// If timer is greater than min, less than max and holding w key
    35.        
    36.         if (timer > timerMax  keyDown == true) {
    37.             rigidbody.AddForce(Physics.gravity * rigidbody.mass * gravityMult * 2f);
    38.         }//If timer is greater than max, holding w key
    39.        
    40.         if (keyDown == false  timer > .001f) {
    41.             rigidbody.AddForce(Physics.gravity * rigidbody.mass * gravityMult * 2f );
    42.         }//If timer is greater than min, not holding key, and in air
    43.        
    44. }//FixedUpdate close
    45. }//Monobehavior close
    46.  
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Frankly, in this case, I don't see why you would split it into two classes. Splitting those two "actions" into method/fuctions? Sure, that would make sense.

    However, you should split into different classes only when it make senses to do so. Will "JumpGravity" and "JumpBase" be mixed with other behaviours? You declared that JumpBase requires JumpGravity... Does another MonoBehaviour requires JumpGravity? If not, maybe it should be a single class. Or maybe it could be a parent class. If those two always work with each other, and never alone, they might be better merged.
     
  3. Aggrojag

    Aggrojag

    Joined:
    Mar 24, 2014
    Posts:
    7
    The only reason I had done that, is so that the force added on the Y axis would not suffer from any delay, as I felt it added a very minor delay when it was combined. This may have been for entirely other reasons, as I've said I'm quite new at this. So the logic would be that it would add the force when W was pushed, and not have much else to worry about. However, when it's combined, in to one class* it had a lot of other script to get around to before checking to see if W was pushed. Also, I don't believe they will be mixed with other behaviors, though it's entirely possible. This is simply a learning project for me so far.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    That makes no sense and it was most likely a bug in your code.