Search Unity

Feedback Which choice would be more efficient when adding a double-jump mechanic?

Discussion in 'Scripting' started by CableEight, Jul 4, 2021.

  1. CableEight

    CableEight

    Joined:
    May 27, 2021
    Posts:
    22
    I'm planning on adding in a double-jump ability to this 2D platformer I've been working on, but I have a scripting question relating to where I should place this function.

    I currently have one "PlayerMovement" script handling x-movement and jumping (including a ground-check), but my plan is to add in a function that checks for a collider trigger ("picking up" an item) which unlocks the ability. This is the entire script (I don't like pasting large amounts of code but this will lead into my main question):

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.  
    4.     private bool jumpKeyWasPressed;
    5.     private float horizontalInput;
    6.     private Rigidbody2D rigidbodyComponent;
    7.     private BoxCollider2D groundColl;
    8.     private bool canDoubleJump;
    9.  
    10.     [SerializeField] private float jumpHeight = 2f;
    11.     [SerializeField] private float moveSpeed = 5f;
    12.     [SerializeField] private LayerMask jumpableGround;
    13.      
    14.     void Start()
    15.     {
    16.         rigidbodyComponent = GetComponent<Rigidbody2D>();
    17.         groundColl = GetComponent<BoxCollider2D>();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (IsGrounded())
    23.         {
    24.             canDoubleJump = true;
    25.         }
    26.  
    27.         if (Input.GetKeyDown(KeyCode.Space))
    28.         {
    29.             //Allows for regular jump
    30.             if (IsGrounded())
    31.             {
    32.                 jumpKeyWasPressed = true;
    33.             }
    34.             //Allows for one double jump
    35.             else
    36.             {
    37.                 if (canDoubleJump)
    38.                 {
    39.                     jumpKeyWasPressed = true;
    40.                     canDoubleJump = false;
    41.                 }
    42.             }
    43.      
    44.         }
    45.  
    46.         //Constantly checks for button presses on movement keys (i.e arrows or WASD)
    47.         horizontalInput = Input.GetAxis("Horizontal");
    48.              
    49.     }
    50.  
    51.     private void FixedUpdate()
    52.     {
    53.         //Handles x-coordinate movement
    54.         rigidbodyComponent.velocity = new Vector2(horizontalInput * moveSpeed, rigidbodyComponent.velocity.y);
    55.  
    56.         //Handles jumping
    57.         if (jumpKeyWasPressed)
    58.         {
    59.             rigidbodyComponent.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    60.             jumpKeyWasPressed = false;
    61.         }
    62.  
    63.     }
    64.  
    65.     //Uses a BoxCast to check if the block-player is grounded
    66.     private bool IsGrounded()
    67.     {
    68.         return Physics2D.BoxCast(groundColl.bounds.center, groundColl.bounds.size, 0f, Vector2.down, .5f, jumpableGround);
    69.     }
    70. }
    With this length, would I still maintain a generally efficient script if I added this extra collision check for upgrade pickups? Or would I be better off creating an entirely new script to handle these pickups?

    If my best option is the latter, how should I go about referencing two different scripts on separate objects?

    Apologies for the lengthy question but any help is appreciated!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    It's usually common to separate your scripts depending on functionality, as keeping scripts as short as possible makes them easier to read. If I were doing this, I would make the pickup in a second script, which has a reference to this script, and increment a counter for how many jumps the player has.